Created
March 30, 2022 11:09
-
-
Save marlon-tucker/2eddb354ca381091a6692164100a8abe to your computer and use it in GitHub Desktop.
Serilog Two Stage Init Infinite Loop Example
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
using Microsoft.Extensions.Caching.Memory; | |
using Serilog.Core; | |
using Serilog.Events; | |
namespace SerilogLoopGist; | |
public class ExampleSink : ILogEventSink | |
{ | |
private readonly IMemoryCache _memoryCache; | |
public ExampleSink(IMemoryCache memoryCache) | |
{ | |
_memoryCache = memoryCache; | |
} | |
public void Emit(LogEvent logEvent) | |
{ | |
// do nothing | |
} | |
} |
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
using Serilog; | |
using Serilog.Core; | |
using Serilog.Events; | |
using SerilogLoopGist; | |
Log.Logger = new LoggerConfiguration() | |
.WriteTo.Console() | |
.CreateBootstrapLogger(); | |
try | |
{ | |
var builder = WebApplication.CreateBuilder(args); | |
builder.Services.AddMemoryCache(); | |
builder.Services.AddSingleton<ILogEventSink, ExampleSink>(); | |
builder.Host.UseSerilog((context, serviceProvider, configuration) => | |
{ | |
configuration | |
.ReadFrom.Configuration(context.Configuration) | |
.ReadFrom.Services(serviceProvider) | |
.Enrich.FromLogContext() | |
.WriteTo.Console(); | |
}); | |
var app = builder.Build(); | |
app.MapGet("/", () => "Hello World!"); | |
app.Run(); | |
return 0; | |
} | |
catch (Exception ex) | |
{ | |
Log.Fatal(ex, ex.Message); | |
return 1; | |
} | |
finally | |
{ | |
Log.CloseAndFlush(); | |
} |
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
<Project Sdk="Microsoft.NET.Sdk.Web"> | |
<PropertyGroup> | |
<TargetFramework>net6.0</TargetFramework> | |
<Nullable>enable</Nullable> | |
<ImplicitUsings>enable</ImplicitUsings> | |
</PropertyGroup> | |
<ItemGroup> | |
<PackageReference Include="Serilog.AspNetCore" Version="5.0.0" /> | |
</ItemGroup> | |
</Project> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment