Created
January 14, 2021 09:02
-
-
Save tanaka-takayoshi/836deefeafe3d3afbbfb269e5bafcd9f to your computer and use it in GitHub Desktop.
This file contains hidden or 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
{ | |
"Serilog": { | |
"Using": [ | |
"Serilog.Sinks.Console", | |
"Serilog.Sinks.File", | |
"NewRelic.LogEnrichers.Serilog" | |
], | |
"MinimumLevel": "Debug", | |
"Enrich": [ "WithNewRelicLogsInContext" ], | |
"WriteTo": [ | |
{ | |
"Name": "File", | |
"Args": { | |
"path": "C:\\temp\\SerilogExample.log.json", | |
"formatter": "NewRelic.LogEnrichers.Serilog.NewRelicFormatter, NewRelic.LogEnrichers.Serilog" | |
} | |
}, | |
{ | |
"Name": "Console", | |
"Args": { | |
"formatter": "NewRelic.LogEnrichers.Serilog.NewRelicFormatter, NewRelic.LogEnrichers.Serilog" | |
} | |
} | |
], | |
"Properties": { | |
"Application": "NewRelic Logging Serilog Example" | |
} | |
} | |
} |
This file contains hidden or 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.AspNetCore.Hosting; | |
using Microsoft.Extensions.Configuration; | |
using Microsoft.Extensions.Hosting; | |
using Microsoft.Extensions.Logging; | |
using NewRelic.LogEnrichers.Serilog; | |
using Serilog; | |
using Serilog.Events; | |
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Linq; | |
using System.Threading.Tasks; | |
namespace NewRelicLabs.WebAppLogging.SerilogInProcessForwarder | |
{ | |
public class Program | |
{ | |
public static IConfiguration Configuration { get; } = new ConfigurationBuilder() | |
.SetBasePath(Directory.GetCurrentDirectory()) | |
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) | |
.AddJsonFile($"appsettings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Production"}.json", optional: true) | |
.AddEnvironmentVariables() | |
.Build(); | |
public static int Main(string[] args) | |
{ | |
Log.Logger = new LoggerConfiguration() | |
.ReadFrom.Configuration(Configuration) | |
.CreateLogger(); | |
try | |
{ | |
Log.Information("Starting web host"); | |
CreateHostBuilder(args).Build().Run(); | |
return 0; | |
} | |
catch (Exception ex) | |
{ | |
Log.Fatal(ex, "Host terminated unexpectedly"); | |
return 1; | |
} | |
finally | |
{ | |
Log.CloseAndFlush(); | |
} | |
} | |
public static IHostBuilder CreateHostBuilder(string[] args) => | |
Host.CreateDefaultBuilder(args) | |
.UseSerilog() | |
.ConfigureWebHostDefaults(webBuilder => | |
{ | |
webBuilder.UseStartup<Startup>(); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment