Skip to content

Instantly share code, notes, and snippets.

@pwelter34
Last active December 14, 2021 03:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pwelter34/edac172a47d6d6f016efb6a1a4df3bca to your computer and use it in GitHub Desktop.
Save pwelter34/edac172a47d6d6f016efb6a1a4df3bca to your computer and use it in GitHub Desktop.
Serilog with azure
{
"Serilog": {
"MinimumLevel": {
"Default": "Debug",
"Override": {
"Microsoft": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"WriteTo": [
{
"Name": "Console",
"Args": {
"outputTemplate": "{Timestamp:HH:mm:ss.fff} [{Level:u1}] {Message:lj}{NewLine}{Exception}"
}
},
{
"Name": "File",
"Args": {
"path": "%HOME%/LogFiles/WebApplication/log.txt",
"rollingInterval": "Day",
"shared": true,
"flushToDiskInterval": "0:0:1",
"outputTemplate": "{Timestamp:HH:mm:ss.fff} [{Level:u1}] {Message:lj}{NewLine}{Exception}"
}
},
{
"Name": "File",
"Args": {
"path": "%HOME%/LogFiles/WebApplication/log.clef",
"rollingInterval": "Day",
"shared": true,
"flushToDiskInterval": "0:0:1",
"formatter": "Serilog.Formatting.Compact.CompactJsonFormatter, Serilog.Formatting.Compact"
}
}
],
"Enrich": [ "FromLogContext", "WithMachineName", "WithThreadId" ],
"Properties": {
"Application": "WebApplication"
}
}
}
public static class Program
{
public static int Main(string[] args)
{
// azure home directory
var homeDirectory = Environment.GetEnvironmentVariable("HOME") ?? ".";
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Verbose()
.MinimumLevel.Override("Microsoft", LogEventLevel.Information)
.Enrich.FromLogContext()
.WriteTo.Console(outputTemplate: "{Timestamp:HH:mm:ss.fff} [{Level:u1}] {Message:lj}{NewLine}{Exception}")
.WriteTo.File(
path: $"{homeDirectory}/LogFiles/WebApplication/boot.txt",
rollingInterval: RollingInterval.Day,
shared: true,
flushToDiskInterval: TimeSpan.FromSeconds(1),
outputTemplate: "{Timestamp:HH:mm:ss.fff} [{Level:u1}] {Message:lj}{NewLine}{Exception}"
)
.WriteTo.File(
formatter: new CompactJsonFormatter(),
path: $"{homeDirectory}/LogFiles/WebApplication/boot.clef",
rollingInterval: RollingInterval.Day,
shared: true,
flushToDiskInterval: TimeSpan.FromSeconds(1)
)
.CreateBootstrapLogger();
try
{
Log.Information("Starting web host");
var host = Host
.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder => webBuilder.UseStartup<Startup>())
.UseSerilog((context, services, configuration) => configuration
.ReadFrom.Configuration(context.Configuration)
.ReadFrom.Services(services)
)
.Build();
host.Run();
return 0;
}
catch (Exception ex)
{
Log.Fatal(ex, "Host terminated unexpectedly");
return 1;
}
finally
{
Log.CloseAndFlush();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment