Sentry has an integration with log4net through the Sentry.Log4Net NuGet package.
Without any code change, this package is able to initialize the Sentry SDK and capture events while including additional properties like
Exception data and more.
Using package manager:
Install-Package Sentry.Log4Net -Version 2.1.1
Or using the .NET Core CLI:
dotnet add package Sentry.Log4Net -v 2.1.1
This package extends
Sentry main SDK. That means besides the log4net
Appender, through this package you’ll also get access to all API and features available in the main
Configuration via XML
Once the log4net integration package is installed in your project, you can modify your configuration file to add the appender.
This can be done, for example, via the
app.config for console and desktop apps or
web.config in case of ASP.NET.
<appender name="SentryAppender" type="Sentry.Log4Net.SentryAppender, Sentry.Log4Net"> <Dsn value="___PUBLIC_DSN___"/> <!--Sends the log event Identity value as the user--> <SendIdentity value="true" /> <threshold value="INFO" /> </appender>
In the example above, the
SendIdentity flag was switched on. The SDK then will take the log4net
Identity value and report to Sentry as the user’s id.
Also in the example above, you can find the DSN being set. That will instruct the
SentryAppender to initialize the SDK.
This is only one of the ways to initialize the SDK. If you wish to configure the SDK programatically, you could leave the DSN out from the appender configuration section. The SDK needs to be initialized only once and since other integrations (like ASP.NET) are also able to initialize the SDK, you only need to pass the DSN to one of these integrations. One common case to not add the DSN to the XML configuration file (which would initialize it via the log4net integration) is to have full access to the SDK option.