Created
January 14, 2021 08:59
-
-
Save tanaka-takayoshi/855bfc1824287ff3ca2c66ee0d3af114 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
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) | |
.Enrich.FromLogContext() | |
.WriteTo.Debug() | |
.WriteTo.Console( | |
formatter: new NewRelicFormatter()) | |
.WriteTo.File( | |
formatter: new NewRelicFormatter(), | |
path: @"C:\temp\SerilogExample.log.json") | |
.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