SharpRaven

repository·develop·Indexed 42 days ago

https://github.com/getsentry/raven-csharp

Legacy .NET client for Sentry designed for .NET Framework 3.5 to 4.6.0. It allows developers to capture exceptions, log messages, and attach metadata to events sent to Sentry. Includes a specific integration for Nancy applications via the SharpRaven.Nancy package.

Tokens
1.5K
Snippets
7
Records
8
Agent score
3%

What's inside SharpRaven

  1. Add additional data to Sentry events

    develop

    There are two primary ways to attach extra context to your events:

    1. Via Exception.Data: Any data added to the Exception.Data property of a thrown exception will be captured and displayed in the extra property in Sentry.
    2. Via SentryEvent: The SentryEvent class allows you to specify ErrorLevel, Fingerprint, a custom Message, and Tags directly.
    try
    {
        // ...    
    }
    catch (Exception exception)
    {
        exception.Data.Add("SomeKey", "SomeValue");
        throw;
    }
  2. Integrate SharpRaven with Nancy

    develop

    The SharpRaven.Nancy package automatically captures the HTTP context and registers on the IPipelines.OnError event to send unhandled exceptions to Sentry.

    To enable it, you must provide a DSN via one of two methods:

    Method 1: Register in TinyIoCContainer

    During application startup, register an instance of the Dsn class.

    Method 2: XML Configuration

    Add a sharpRaven section to your web.config or app.config.

    Once configured, you can access the client by requesting IRavenClient in your Nancy modules.

    // Container registration
    protected override void ApplicationStartup(TinyIoCContainer container, IPipelines pipelines)
    {
        container.Register(new Dsn("https://public@sentry.io/project-id"));
    }
    <!-- XML Configuration -->
    <configuration>
      <configSections>
        <section name="sharpRaven" type="SharpRaven.Nancy.NancyConfiguration, SharpRaven.Nancy" />
      </configSections>
      <sharpRaven>
        <dsn value="https://public@sentry.io/project-id" />
      </sharpRaven>
    </configuration>
    // Using IRavenClient in a module
    public class LoggingModule : NancyModule
    {
        private readonly IRavenClient ravenClient;
    
        public LoggingModule(IRavenClient ravenClient)
        {
            this.ravenClient = ravenClient;
        }
    }
  3. Install SharpRaven via NuGet

    develop

    You can install the SharpRaven client using NuGet packages. For standard applications, use the SharpRaven package. For Nancy applications, use SharpRaven.Nancy.

    # Install the core client
    nuget install SharpRaven
    
    # Or install the Nancy integration
    nuget install SharpRaven.Nancy
  4. Capture exceptions and messages

    develop

    You can send both actual exceptions and arbitrary text messages to Sentry using the Capture method with a SentryEvent object.

    Capture an Exception

    Wrap your code in a try-catch block and pass the exception to ravenClient.Capture.

    Capture a Message

    Pass a string to the SentryEvent constructor to log a non-exception message.

    // Capturing an exception
    try
    {
        int i2 = 0;
        int i = 10 / i2;
    }
    catch (Exception exception)
    {
        ravenClient.Capture(new SentryEvent(exception));
    }
    
    // Capturing a message
    ravenClient.Capture(new SentryEvent("Hello World!"));
  5. Debug and intercept RavenClient events

    develop

    You can hook into the RavenClient lifecycle to debug internal errors or manipulate data before it is sent to Sentry.

    Handle internal errors

    Use ErrorOnCapture to define custom logic that executes if an exception occurs within RavenClient itself. By default, these are logged to the Console.

    Manipulate data before sending

    Use the BeforeSend delegate to inspect or modify the SentryEvent (the requester) before it is transmitted to the Sentry server.

    // Handle internal RavenClient errors
    ravenClient.ErrorOnCapture = exception =>
    {
        // Custom code here
    };
    
    // Intercept and modify data before it is sent
    ravenClient.BeforeSend = requester =>
    {
        // Inspect or replace the requester
        return requester;
    };