Skip to content

Commit

Permalink
Rework sentry with serilog
Browse files Browse the repository at this point in the history
Signed-off-by: Jan Jansen <jan.jansen@gdata.de>
  • Loading branch information
farodin91 committed Dec 7, 2021
1 parent c83c0a8 commit e5da070
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 13 deletions.
2 changes: 1 addition & 1 deletion shared.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project>

<PropertyGroup>
<Version>0.9.2</Version>
<Version>0.9.3</Version>
<LangVersion>10</LangVersion>
<Nullable>enable</Nullable>
<WarningsAsErrors>CS8600;CS8602;CS8625;CS8618;CS8604;CS8601</WarningsAsErrors>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
using System;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Motor.Extensions.Diagnostics.Sentry;
using Motor.Extensions.Utilities.Abstractions;
using Sentry;
using Sentry.Serilog;
using Serilog;
using Serilog.Formatting.Json;

Expand All @@ -19,22 +19,27 @@ public static IMotorHostBuilder ConfigureSerilog(this IMotorHostBuilder hostBuil
.ConfigureSentry()
.UseSerilog((hostingContext, loggerConfiguration) =>
{
var sentryOptions = new SentrySerilogOptions();
hostingContext.Configuration.GetSection("Sentry").Bind(sentryOptions);
loggerConfiguration
.ReadFrom.Configuration(hostingContext.Configuration)
.Enrich.FromLogContext()
.WriteTo.Console(new JsonFormatter(renderMessage: true));
.WriteTo.Console(new JsonFormatter(renderMessage: true))
.WriteTo.Sentry(opts =>
{
opts.Dsn = sentryOptions.Dsn;
opts.MinimumEventLevel = sentryOptions.MinimumEventLevel;
opts.MinimumBreadcrumbLevel = sentryOptions.MinimumBreadcrumbLevel;
opts.InitializeSdk = false;
});
configuration?.Invoke(hostingContext, loggerConfiguration);
})
.ConfigureServices((_, services) =>
{
services.AddLogging(loggingBuilder =>
{
var builder = loggingBuilder
loggingBuilder
.AddSerilog(dispose: true);
if (SentrySdk.IsEnabled)
{
builder.AddSentry(o => o.InitializeSdk = false);
}
});
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using Microsoft.Extensions.Options;
using Motor.Extensions.Utilities.Abstractions;
using Sentry;
using Sentry.Extensions.Logging;
using Sentry.Serilog;
using MSOptions = Microsoft.Extensions.Options.Options;

namespace Motor.Extensions.Diagnostics.Sentry;
Expand All @@ -15,10 +15,10 @@ public static IMotorHostBuilder ConfigureSentry(this IMotorHostBuilder hostBuild
return hostBuilder
.ConfigureServices((context, services) =>
{
var sentryOptions = new SentryLoggingOptions();
var sentryOptions = new SentrySerilogOptions();
context.Configuration.GetSection("Sentry").Bind(sentryOptions);
SentrySdk.Init(sentryOptions);
services.AddTransient<IOptions<SentryLoggingOptions>>(_ => MSOptions.Create(sentryOptions));
services.AddTransient<IOptions<SentrySerilogOptions>>(_ => MSOptions.Create(sentryOptions));
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Hosting" Version="6.0.0" />
<PackageReference Include="Sentry.Extensions.Logging" Version="3.12.0" />
<PackageReference Include="Sentry.Serilog" Version="3.12.1" />
</ItemGroup>

<ItemGroup>
Expand Down

0 comments on commit e5da070

Please sign in to comment.