Skip to content
This repository has been archived by the owner on Aug 30, 2023. It is now read-only.

Add support for .NET Core #125

Closed
polarina opened this issue Jun 14, 2016 · 128 comments
Closed

Add support for .NET Core #125

polarina opened this issue Jun 14, 2016 · 128 comments

Comments

@polarina
Copy link

Hi,

Is it possible to get the ability to use this library with .NET Core?

Thanks in advance!

@polarina polarina changed the title Support for .NET Core Add support for .NET Core Jun 14, 2016
@gtarsia
Copy link

gtarsia commented Aug 8, 2016

@polarina I tried porting it to core, but it felt like non-trivial. Looks like the CoreFX project still needs to update some legacy C# stuff for SharpRaven to work (for example: adding Environment.UserName dotnet/corefx#9851).
Maybe someone with more experience on .NET can make a port.

It was thinking of manually sending HTTP requests to sentry to avoid using SharpRaven, but I don't see a tutorial for that. I guess I'll make the javascript report server errors since I'm doing an AspNet site.

@sabiland
Copy link

sabiland commented Sep 7, 2016

Anything new about .NET Core support?

@wakawaka54
Copy link

Any update on what needs to be done? I'm looking through the codebase now.

@asbjornu
Copy link
Contributor

@wakawaka54 I have no idea, to be honest. I've not started using .NET Core in any projects yet, so I don't know what the requirements are.

@timorzadir
Copy link
Contributor

@wakawaka54
Copy link

Just FYI, I made a .NET Core port.

.NET Core RavenSharp Port

You can add it to your .Net Core project.json file as:

dependencies: {
    "RavenSharp.Core": "1.0.0-beta-1"
}

Then add the configuration to your appsettings.json file as:

  "RavenOptions": {
    "DSN": "dsnfromsentry"
  }

You need to configure your Startup.cs as follows:

        public void ConfigureServices(IServiceCollection services)
{
            services.Configure<RavenOptions>(Configuration.GetSection("RavenOptions"));

            //Add HTTPContextAccessor as Singleton
            services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();

            //Configure RavenClient
            services.AddScoped<IRavenClient, RavenClient>((s) => {

                var rc = new RavenClient(s.GetRequiredService<IOptions<RavenOptions>>(), s.GetRequiredService<IHttpContextAccessor>())
                {
                    Environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT")
                };
                return rc;
            });
}

You can then inject the IRavenClient interface into where ever you want to use it.

I am not sure how we are going to merge this into the existing RavenSharp code base because I had to rewrite a lot of stuff. The tests had legacy C# stuff in them and the HttpContext stuff has all changed so I had to change all that. But that new port works well for me and I don't have any issues.

@ricardoalcantara
Copy link

ricardoalcantara commented Sep 22, 2016

@wakawaka54 I had the same idea of porting and my code end up very much like yours, but I got very upset tryng to make it compatible with net45, net451, netstandard15 and netstandard16, a lot of things I had comment like you did and I also had to rewrite the Requester, so I thought that it wouldn't be any far for been candidate to get merged. So I decided to create a 'Light' Version myself: RavenSharp Light

I am still working on it and right now I am writing more information at the README.md. I copied the IRavenClient and implemented it all so you could also inject it on top the RavenSharp implementation.

@asbjornu
Copy link
Contributor

@wakawaka54: Awesome work! We can merge your changes into a new branch for now and publish releases from it with a new major version number. Then as we get feedback and feel the .NET Core is stable and sturdy, we can move the develop branch over to it and branch off from the current to a release/2.x branch. Just submit a PR and I'll work out the rest.

@sabiland
Copy link

@wakawaka54 great, it works without any problems! :)

@ricardoalcantara
Copy link

@asbjornu Cool. Maybe I was wrong about what I thought.

@ricardoalcantara
Copy link

ricardoalcantara commented Sep 22, 2016

@wakawaka54 I would like to help porting the code too, if you need. My fork already compiles to the old .NET (NET45, NET451, NET46) and it also works very much like yours on netstandard1.6. The module part was removed at netstandard1.5, so I had to #if and remove it, so after your PR I could also help with that multi-platform compatibility. maybe!

@wakawaka54
Copy link

wakawaka54 commented Sep 22, 2016

@asbjornu Okay a new branch sounds good. I can help rewrite and implement some of the tests I had to comment out. Mostly the tests that use the TestServer stuff doesn't work at all because of all sorts of reasons. But DotNetCore has the new Microsoft.AspNetCore.TestServer stuff which should make that pretty easy to re-implement.

Are you going to make a new branch for me to merge into?

@wakawaka54
Copy link

@ricardoalcantara Okay sure. I like the idea of making a "light" version. However, Sentry.io has some requirements that they like their APIs to meet which is why you have all the Log Scrubbing stuff on there. It would be more of a challenge to get the .Net Core stuff to be backwards compatible with a lot of the older .Net releases, I am not sure if that's worth the extra effort.

@ricardoalcantara
Copy link

ricardoalcantara commented Sep 22, 2016

@wakawaka54 I meant I literally forked the Raven-CSharp . I stopped trying to migrate because I had a lot of troubles with Nancy, and I had to comment few things which I didn't feel comfortable with. I also didn't migrate the configuration you have done.

@asbjornu
Copy link
Contributor

I've created a release/3.0 branch that you can pull request against, @wakawaka54.

@jamiecounsell
Copy link

@wakawaka54
Copy link

Yeah that's something to look into. The biggest difficulty that I see with RavenSharp and using .NET Standard is HttpContext. I haven't used the earlier versions of HttpContext but diving into RavenSharp, I saw a lot of stuff that was different between that and .NET Core. If someone could look at the HttpContext in .NET Standard and see if it'll work for both ASP.NET and .NET Core, then adopting it would be straight forward.

@sabiland
Copy link

sabiland commented Sep 28, 2016

@wakawaka54

Using this configuration:

        public void ConfigureServices(IServiceCollection services)
{
            services.Configure<RavenOptions>(Configuration.GetSection("RavenOptions"));

            //Add HTTPContextAccessor as Singleton
            services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();

            //Configure RavenClient
            services.AddScoped<IRavenClient, RavenClient>((s) => {

                var rc = new RavenClient(s.GetRequiredService<IOptions<RavenOptions>>(), s.GetRequiredService<IHttpContextAccessor>())
                {
                    Environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT")
                };
                return rc;
            });
}

It seems only 1 sentry message is captured per application start. First capture is sent to Sentry, every another one not. Something wrong with raven services configuration?

EDIT

The problem was this:

I had this version in my project.json
"RavenSharp.Core": "1.0.0"

With this one it works ok!
"RavenSharp.Core": "1.0.0-beta-1"

@consigliory
Copy link

+1

@wakawaka54
Copy link

@sabiland Yeah my bad, I'm a Nuget Noob and didn't think about it too much while I was uploading the package 😞

@sabiland
Copy link

@wakawaka54 So it still has to be "RavenSharp.Core": "1.0.0-beta-1" right?

@AlexanderNZ
Copy link

AlexanderNZ commented Oct 10, 2016

Hi all, question for anyone who has got this working,

I've followed @wakawaka54 's instructions, added the snippets in the right place.
When I run dotnet build I get

error CS0246: The type or namespace name 'RavenSharp' could not be found (are you missing a using directive or an assembly reference?)

I've added using RavenSharp.Core; to my imports - should I have added something else instead?

@asbjornu
Copy link
Contributor

@AlexanderNZ: I haven't looked at the code and haven't even compiled a .NET Core project yet, but as the name of the project is SharpRaven and not RavenSharp, might that be what the issue here is?

@AlexanderNZ
Copy link

I thought that too @asbjornu - I tried SharpRaven.Core initially, but that spawns more errors:
https://puu.sh/rECIC/64a765acc3.png

In the end I pulled RavenSharp as that was the dependency name - "RavenSharp.Core": 1.0.0-beta-1

I'm sure I'm just importing wrong, but I can't find any reference to how to import and use RavenSharp.Core. The fact that I'm not a C# guy doesn't help haha

@raRaRa
Copy link

raRaRa commented Dec 4, 2016

Any updates on this? I would really like to use Sentry for my .NET core web application.

@asbjornu
Copy link
Contributor

asbjornu commented Dec 5, 2016

@raRaRa: We have #161 and #163 that tries to tackle the problem, but none of them are completed. I don't know what @wakawaka54 and @reflectiondm can say about the current status?

@LordMike
Copy link

LordMike commented Feb 3, 2017

Is there a plan to make implementations of ILogger and friends from Microsoft.Extensions.Logging?

EDIT: I added this as a separate issue.

@knocte
Copy link

knocte commented Feb 6, 2018

@jose-cf FWIW in my case I'm using https://github.com/ricardoalcantara/raven-csharp-light in the meantime.

@masaanli
Copy link

masaanli commented Feb 6, 2018

When coming? Almost :)

@jjradke
Copy link

jjradke commented Mar 2, 2018

Any update on this? We are also working on porting all our apps to Net Standard 2 for the libraries, and Net Core 2 for the apps; and really want to continue using Raven!

@asbjornu
Copy link
Contributor

asbjornu commented Mar 2, 2018

Since #189 is merged, we now only need to get an AppVeyor build up and running. I'm working on it, but any help would be appreciated.

@jjradke
Copy link

jjradke commented Mar 2, 2018

How will we know which version it will be that will have this?

Will it work for .NET Standard 2.0, and .NET Core 2.0 apps without requiring any framework modules to be added to the solution?

@asbjornu
Copy link
Contributor

asbjornu commented Mar 2, 2018

I think #197 gives us what we want. I'll merge that and try to get a new pre-release package out over the next few days.

@jjradke
Copy link

jjradke commented Mar 3, 2018

Excellent! When it is available via NUGET, post on here, and I will pull it down and test it a lot next week as I am working on/testing my code migration!

THANKS!!!

@asbjornu
Copy link
Contributor

asbjornu commented Mar 4, 2018

SharpRaven version 2.2.1-unstable0054 was just published to NuGet.org. Please test it and report back your status. Thanks!

@jjradke
Copy link

jjradke commented Mar 9, 2018

I have been testing it out this week on Net Standard 2.0 DLL's being used within a .NET Framework 4.6.2 application, as well as from within .NET Core 2.0 Microservices running within Docker on Linux, and everything has been running as expected, with appropriate logging being posted!

@asbjornu
Copy link
Contributor

asbjornu commented Mar 9, 2018

@jjradke: I'm glad it works! Given your feedback and the 236 downloads of the beta so far, I think we can declare this done and done. Closing this as complete. Thanks for the patience everyone, I'm happy we finally nailed this and that you stuck with it for so long.

@asbjornu asbjornu closed this as completed Mar 9, 2018
@davidroth
Copy link

Cool! @asbjornu any eta for 2.2.1 nuget without -unstable?

@asbjornu
Copy link
Contributor

@davidroth: Minus 60 seconds. 😄 2.3 is out the door.

@webchetan
Copy link

@asbjornu Thank you. Looks like its working. :)

@Newmski
Copy link

Newmski commented Apr 12, 2018

Are there any examples that show how to set this up using ASP Core 2? e.g. ConfigureServices

@bruno-garcia
Copy link
Member

bruno-garcia commented Apr 12, 2018

I've just tested it and it worked:

using System;
using System.Collections.Generic;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;
using SharpRaven;
using SharpRaven.Data;

namespace AspNetCore
{
    [Route("/throw")]
    public class Program : Controller
    {
        [HttpGet("null")]
        public void Get() => throw null;

        public static void Main(string[] args)
        {
            BuildWebHost(args).Run();
        }

        public static IWebHost BuildWebHost(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .ConfigureServices(s =>
                {
                    s.AddMvc();
                    s.AddSingleton<IRavenClient>(
                        new RavenClient(
                            dsn));
                })
                .Configure(a =>
                {
                    a.Use(async (context, next) =>
                    {
                        try
                        {
                            await next.Invoke();
                        }
                        catch (Exception e)
                        {
                            var client = context.RequestServices.GetService<IRavenClient>();
                            if (client != null)
                            {
                                var id = await client.CaptureAsync(new SentryEvent(e));
                                if (id != null && !context.Response.HasStarted)
                                {
                                    context.Response.Headers.TryAdd("X-Sentry-Id", id);
                                }
                            }

                            throw;
                        }
                    });
                    a.UseMvc();
                })
                .Build();
    }
}

Please note that Sentry will be working on providing a better experience for ASP.NET Core integration.
Basically what one would expect: .AddSentry, .UseSentry kind of experience.

@Newmski
Copy link

Newmski commented Apr 13, 2018

@bruno-garcia Thanks, that works

@razfriman
Copy link

@bruno-garcia - Using your example, I created an extension class that will add that functionality

using System;
using System.Collections.Generic;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using SharpRaven.Data;

namespace SharpRaven.Extensions
{
    public static class SentryExtensions
    {
        public static void UseSentry(this IApplicationBuilder builder)
        {
            builder.Use(async (context, next) =>
            {
                try
                {
                    await next.Invoke();
                }
                catch (Exception e)
                {
                    var client = context.RequestServices.GetService<IRavenClient>();
                    if (client != null)
                    {
                        var id = await client.CaptureAsync(new SentryEvent(e));
                        if (id != null && !context.Response.HasStarted)
                        {
                            context.Response.Headers.TryAdd("X-Sentry-Id", id);
                        }
                    }

                    throw;
                }
            });
        }
    }
}

startup.cs.Configure()

...
app.UseSentry();
...

@bruno-garcia
Copy link
Member

@razfriman our goal is to provide a package which will give you that experience. It'll be coming soon.

@roryprimrose
Copy link

@bruno-garcia Is that going to be a separate package specifically for ASP.Net core? Will it also populate sentry reports with request/response data?

@bruno-garcia
Copy link
Member

I've only been involved with the project for about a month but from what I see in the code:
https://github.com/getsentry/raven-csharp/blob/develop/src/app/SharpRaven/Data/HttpRequestBodyConverter.cs#L55

It already has support to extracting the payload.

I also believe I read that the support to dynamic was introduced so that HttpContext from System.Web (classic ASP.NET) and from ASP.NET Core would work as relevant members are the same and it would avoid repending on both assemblies nor require breaking the library up.

All of this could probably be validate by @asbjornu, but he's on vacation :D

@asbjornu
Copy link
Contributor

@bruno-garcia, that’s correct. :)

@roryprimrose
Copy link

@bruno-garcia Thanks for the feedback. I've started to integrate this into my asp.net core project.

How do I integrate HttpRequestBodyConverter into the middleware example above? I also want to populate the authenticated user from the execution context.

@bruno-garcia
Copy link
Member

bruno-garcia commented Jun 28, 2018

We've just released the first preview of the ASP.NET Core integration. It also brings in the Microsoft.Extensions.Logging so that your Informational log messages in the same transaction are sent as breadcrumbs.

I would love to get some feedback as we work toward a major release:

It's on NuGet: https://www.nuget.org/packages/Sentry.AspNetCore/

There's a video showing how to integrate in your solution: https://www.youtube.com/watch?v=xK6a1goK_w0

Release on GitHub:
https://github.com/getsentry/sentry-dotnet/releases

Docs:
https://github.com/getsentry/sentry-dotnet/

I'm online on Gitter: https://gitter.im/getsentry/dotnet

@edu-eng
Copy link

edu-eng commented Aug 8, 2018

Do you have any support for previous versions of .Net? 2.0 more precisely webforms?

@bruno-garcia
Copy link
Member

SharpRaven supports anything from .NET Framework 3.5 all the way to .NET Standard 2.0. Although for the latter we advise using our new SDK instead.

@edu-eng
Copy link

edu-eng commented Aug 8, 2018 via email

@bruno-garcia
Copy link
Member

Using SharpRaven with ASP.NET WebForms is a common use case.
This topic is unrelated to this issue so please raise a new issue to discuss it.

@getsentry getsentry locked as resolved and limited conversation to collaborators Aug 8, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests