| | 1 | | using OpenTelemetry.Logs; |
| | 2 | | using Serilog; |
| | 3 | | using Serilog.Sinks.OpenTelemetry; |
| | 4 | |
|
| | 5 | | namespace Api.ServiceExtension; |
| | 6 | | public static class LoggingCollectionExtensions { |
| 0 | 7 | | public static IServiceCollection AddLoggerWithSeq(this IServiceCollection services, IConfiguration configuration) { |
| | 8 | |
|
| 0 | 9 | | Log.Logger = new LoggerConfiguration() |
| 0 | 10 | | .Enrich.FromLogContext() |
| 0 | 11 | | .MinimumLevel.Information() |
| 0 | 12 | | .WriteTo.Console() |
| 0 | 13 | | .WriteTo.OpenTelemetry(otlp => { |
| 0 | 14 | | otlp.Endpoint = "http://seq:80/ingest/otlp/v1/logs"; |
| 0 | 15 | | otlp.Protocol = OtlpProtocol.HttpProtobuf; |
| 0 | 16 | | otlp.Headers = new Dictionary<string, string> { |
| 0 | 17 | | ["X-Seq-ApiKey"] = "INSERT_KEY" |
| 0 | 18 | | }; |
| 0 | 19 | | otlp.ResourceAttributes = new Dictionary<string, object> { |
| 0 | 20 | | ["service.name"] = "Northwind-Api" |
| 0 | 21 | | }; |
| 0 | 22 | | }) |
| 0 | 23 | | .ReadFrom.Configuration(configuration) |
| 0 | 24 | | .CreateLogger(); |
| | 25 | |
|
| 0 | 26 | | services.AddLogging(logging => { |
| 0 | 27 | | logging.ClearProviders(); |
| 0 | 28 | | logging.AddSerilog(Log.Logger, dispose: true); |
| 0 | 29 | | logging.AddOpenTelemetry(x => x.AddOtlpExporter()); |
| 0 | 30 | | }); |
| | 31 | |
|
| 0 | 32 | | return services; |
| 0 | 33 | | } |
| | 34 | |
|
| 1 | 35 | | public static IServiceCollection AddDefaultLogger(this IServiceCollection services, IConfiguration configuration) { |
| | 36 | |
|
| 1 | 37 | | Log.Logger = new LoggerConfiguration() |
| 1 | 38 | | .ReadFrom.Configuration(configuration) |
| 1 | 39 | | .Enrich.FromLogContext() |
| 1 | 40 | | .CreateLogger(); |
| | 41 | |
|
| 2 | 42 | | services.AddLogging(logging => { |
| 1 | 43 | | logging.ClearProviders(); |
| 1 | 44 | | logging.AddSerilog(Log.Logger, dispose: true); |
| 2 | 45 | | }); |
| 1 | 46 | | return services; |
| 1 | 47 | | } |
| | 48 | |
|
| | 49 | | } |