-
-
Notifications
You must be signed in to change notification settings - Fork 213
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #145 from getsentry/feature/serilog-breadcrumbs
feat(serilog): Breadcrumbs and Events
- Loading branch information
Showing
14 changed files
with
467 additions
and
130 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
using System; | ||
using Microsoft.AspNetCore; | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Logging; | ||
using Sentry.Serilog; | ||
using Serilog; | ||
using Serilog.Events; | ||
|
||
namespace Sentry.Samples.AspNetCore.Serilog | ||
{ | ||
public class Program | ||
{ | ||
public static void Main(string[] args) => BuildWebHost(args).Run(); | ||
|
||
public static IWebHost BuildWebHost(string[] args) => | ||
WebHost.CreateDefaultBuilder(args) | ||
|
||
.UseSerilog((h, c) => | ||
c.Enrich.FromLogContext() | ||
.MinimumLevel.Debug() | ||
.WriteTo.Console() | ||
// Add Sentry integration with Serilog | ||
// Two levels are used to configure it. | ||
// One sets which log level is minimally required to keep a log message as breadcrumbs | ||
// The other sets the minimum level for messages to be sent out as events to Sentry | ||
.WriteTo.Sentry(s => | ||
{ | ||
s.MinimumBreadcrumbLevel = LogEventLevel.Debug; | ||
s.MinimumEventLevel = LogEventLevel.Error; | ||
})) | ||
|
||
// Add Sentry integration | ||
// It can also be defined via configuration (including appsettings.json) | ||
// or coded explicitly, via parameter like: | ||
// .UseSentry("dsn") or .UseSentry(o => o.Dsn = ""; o.Release = "1.0"; ...) | ||
.UseSentry() | ||
|
||
// The App: | ||
.Configure(a => | ||
{ | ||
// An example ASP.NET Core middleware that throws an | ||
// exception when serving a request to path: /throw | ||
a.Use(async (context, next) => | ||
{ | ||
// See MinimumBreadcrumbLevel set at the Serilog configuration above | ||
Log.Logger.Debug("Static Serilog logger debug log stored as breadcrumbs."); | ||
|
||
var log = context.RequestServices.GetService<ILoggerFactory>() | ||
.CreateLogger<Program>(); | ||
|
||
log.LogInformation("Handling some request..."); | ||
|
||
// Sends an event which includes the info and debug messages above | ||
Log.Logger.Error("Logging using static Serilog directly also goes to Sentry."); | ||
|
||
if (context.Request.Path == "/throw") | ||
{ | ||
var hub = context.RequestServices.GetService<IHub>(); | ||
hub.ConfigureScope(s => | ||
{ | ||
// More data can be added to the scope like this: | ||
s.SetTag("Sample", "ASP.NET Core"); // indexed by Sentry | ||
s.SetExtra("Extra!", "Some extra information"); | ||
}); | ||
|
||
// Logging through the ASP.NET Core `ILogger` while using Serilog | ||
log.LogInformation("Logging info..."); | ||
log.LogWarning("Logging some warning!"); | ||
|
||
// The following exception will be captured by the SDK and the event | ||
// will include the Log messages and any custom scope modifications | ||
// as exemplified above. | ||
throw new Exception("An exception thrown from the ASP.NET Core pipeline"); | ||
} | ||
|
||
await next(); | ||
}); | ||
}) | ||
.Build(); | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
samples/Sentry.Samples.AspNetCore.Serilog/Properties/launchSettings.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
{ | ||
"iisSettings": { | ||
"windowsAuthentication": false, | ||
"anonymousAuthentication": true, | ||
"iisExpress": { | ||
"applicationUrl": "http://localhost:59539/", | ||
"sslPort": 0 | ||
} | ||
}, | ||
"profiles": { | ||
"IIS Express": { | ||
"commandName": "IISExpress", | ||
"launchBrowser": true, | ||
"launchUrl": "throw", | ||
"environmentVariables": { | ||
"ASPNETCORE_ENVIRONMENT": "Development" | ||
} | ||
}, | ||
"Sentry.Samples.AspNetCore.Basic": { | ||
"commandName": "Project", | ||
"launchBrowser": true, | ||
"launchUrl": "throw", | ||
"environmentVariables": { | ||
"ASPNETCORE_ENVIRONMENT": "Development" | ||
}, | ||
"applicationUrl": "http://localhost:59540/" | ||
} | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
samples/Sentry.Samples.AspNetCore.Serilog/Sentry.Samples.AspNetCore.Serilog.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<Project Sdk="Microsoft.NET.Sdk.Web"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netcoreapp2.1</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.AspNetCore" Version="2.1.2" /> | ||
<PackageReference Include="Serilog.Sinks.Console" Version="3.1.1" /> | ||
<PackageReference Include="Serilog.AspNetCore" Version="2.1.1" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\Sentry.AspNetCore\Sentry.AspNetCore.csproj" /> | ||
<ProjectReference Include="..\..\src\Sentry.Serilog\Sentry.Serilog.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
21 changes: 21 additions & 0 deletions
21
samples/Sentry.Samples.AspNetCore.Serilog/appsettings.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
{ | ||
// Example configuration via JSON. Only a subset of the settings below: | ||
// All Sentry settings can also be configured via code or environment variables: | ||
"Sentry": { | ||
// The DSN can also be set via environment variable | ||
"Dsn": "https://5fd7a6cda8444965bade9ccfd3df9882@sentry.io/1188141", | ||
// Opt-in for payload submission | ||
"IncludeRequestPayload": true, | ||
// Sends Cookies, User Id when one is logged on and user IP address to sentry. It's turned off by default. | ||
"SendDefaultPii": true, | ||
// Whether to add System.Diagnostics.Activity data to the event:: | ||
// For more: https://github.com/dotnet/corefx/blob/master/src/System.Diagnostics.DiagnosticSource/src/ActivityUserGuide.md | ||
"IncludeActivityData": true, | ||
// Send the stack trace of captured messages (e.g: a LogWarning without an exception) | ||
"AttachStackTrace": true, | ||
// The flag below can be used to see the internal logs of the SDK in the applications log (it's off by default) | ||
"Debug": true, | ||
// By default the level is Debug but it can be changed to any level of SentryLevel enum | ||
"DiagnosticsLevel": "Error" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
using Sentry.Protocol; | ||
using Serilog.Events; | ||
|
||
namespace Sentry.Serilog | ||
{ | ||
internal static class LogLevelExtensions | ||
{ | ||
public static SentryLevel? ToSentryLevel(this LogEventLevel loggingLevel) | ||
{ | ||
switch (loggingLevel) | ||
{ | ||
case LogEventLevel.Fatal: | ||
return SentryLevel.Fatal; | ||
case LogEventLevel.Error: | ||
return SentryLevel.Error; | ||
case LogEventLevel.Warning: | ||
return SentryLevel.Warning; | ||
case LogEventLevel.Information: | ||
return SentryLevel.Info; | ||
case LogEventLevel.Debug: | ||
return SentryLevel.Debug; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
public static BreadcrumbLevel ToBreadcrumbLevel(this LogEventLevel level) | ||
{ | ||
switch (level) | ||
{ | ||
case LogEventLevel.Verbose: | ||
case LogEventLevel.Debug: | ||
return BreadcrumbLevel.Debug; | ||
case LogEventLevel.Information: | ||
return BreadcrumbLevel.Info; | ||
case LogEventLevel.Warning: | ||
return BreadcrumbLevel.Warning; | ||
case LogEventLevel.Error: | ||
return BreadcrumbLevel.Error; | ||
case LogEventLevel.Fatal: | ||
return BreadcrumbLevel.Critical; | ||
default: | ||
return (BreadcrumbLevel)level; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
using System; | ||
using Sentry.Protocol; | ||
using Serilog.Events; | ||
|
||
namespace Sentry.Serilog | ||
{ | ||
/// <summary> | ||
/// Sentry Options for Serilog logging | ||
/// </summary> | ||
/// <inheritdoc /> | ||
public class SentrySerilogOptions : SentryOptions | ||
{ | ||
/// <summary> | ||
/// Whether to initialize this SDK through this integration | ||
/// </summary> | ||
public bool InitializeSdk { get; set; } = true; | ||
|
||
/// <summary> | ||
/// Minimum log level to send an event. | ||
/// </summary> | ||
/// <remarks> | ||
/// Events with this level or higher will be sent to Sentry. | ||
/// </remarks> | ||
/// <value> | ||
/// The minimum event level. | ||
/// </value> | ||
public LogEventLevel MinimumEventLevel { get; set; } | ||
|
||
/// <summary> | ||
/// Minimum log level to record a breadcrumb. | ||
/// </summary> | ||
/// <remarks>Events with this level or higher will be stored as <see cref="Breadcrumb"/></remarks> | ||
/// <value> | ||
/// The minimum breadcrumb level. | ||
/// </value> | ||
public LogEventLevel MinimumBreadcrumbLevel { get; set; } | ||
|
||
/// <summary> | ||
/// Optional <see cref="IFormatProvider"/> | ||
/// </summary> | ||
public IFormatProvider FormatProvider { get; set; } | ||
} | ||
} |
Oops, something went wrong.