Add Seq to WebMVC
This commit is contained in:
parent
9d7124ba4d
commit
77debf821b
@ -3,39 +3,74 @@ using Microsoft.AspNetCore.Hosting;
|
|||||||
using Microsoft.Extensions.Configuration;
|
using Microsoft.Extensions.Configuration;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
using Serilog;
|
using Serilog;
|
||||||
|
using System;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
|
|
||||||
namespace Microsoft.eShopOnContainers.WebMVC
|
namespace Microsoft.eShopOnContainers.WebMVC
|
||||||
{
|
{
|
||||||
public class Program
|
public class Program
|
||||||
{
|
{
|
||||||
public static void Main(string[] args)
|
public static readonly string AppName = typeof(Program).Namespace;
|
||||||
|
public static readonly string ShortAppName = AppName.Substring(AppName.LastIndexOf('.', AppName.LastIndexOf('.') - 1) + 1);
|
||||||
|
|
||||||
|
public static int Main(string[] args)
|
||||||
{
|
{
|
||||||
BuildWebHost(args).Run();
|
var configuration = GetConfiguration();
|
||||||
|
|
||||||
|
Log.Logger = CreateSerilogLogger(configuration);
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
Log.Information("Configuring web host ({Application})...", AppName);
|
||||||
|
var host = BuildWebHost(configuration, args);
|
||||||
|
|
||||||
|
Log.Information("Starting web host ({Application})...", AppName);
|
||||||
|
host.Run();
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Log.Fatal(ex, "Program terminated unexpectedly ({Application})!", AppName);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
Log.CloseAndFlush();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static IWebHost BuildWebHost(string[] args) =>
|
private static IWebHost BuildWebHost(IConfiguration configuration, string[] args) =>
|
||||||
WebHost.CreateDefaultBuilder(args)
|
WebHost.CreateDefaultBuilder(args)
|
||||||
.UseContentRoot(Directory.GetCurrentDirectory())
|
.CaptureStartupErrors(false)
|
||||||
.UseStartup<Startup>()
|
.UseStartup<Startup>()
|
||||||
.ConfigureAppConfiguration((builderContext, config) =>
|
|
||||||
{
|
|
||||||
config.AddEnvironmentVariables();
|
|
||||||
})
|
|
||||||
.ConfigureLogging((hostingContext, builder) =>
|
|
||||||
{
|
|
||||||
builder.AddConfiguration(hostingContext.Configuration.GetSection("Logging"));
|
|
||||||
builder.AddConsole();
|
|
||||||
builder.AddDebug();
|
|
||||||
})
|
|
||||||
.UseApplicationInsights()
|
.UseApplicationInsights()
|
||||||
.UseSerilog((builderContext, config) =>
|
.UseConfiguration(configuration)
|
||||||
{
|
.UseSerilog()
|
||||||
config
|
|
||||||
.MinimumLevel.Information()
|
|
||||||
.Enrich.FromLogContext()
|
|
||||||
.WriteTo.Console();
|
|
||||||
})
|
|
||||||
.Build();
|
.Build();
|
||||||
|
|
||||||
|
private static Serilog.ILogger CreateSerilogLogger(IConfiguration configuration)
|
||||||
|
{
|
||||||
|
var seqServerUrl = configuration["Serilog:SeqServerUrl"];
|
||||||
|
|
||||||
|
return new LoggerConfiguration()
|
||||||
|
.MinimumLevel.Verbose()
|
||||||
|
.Enrich.WithProperty("Application", AppName)
|
||||||
|
.Enrich.FromLogContext()
|
||||||
|
.WriteTo.Console()
|
||||||
|
.WriteTo.Seq(string.IsNullOrWhiteSpace(seqServerUrl) ? "http://seq" : seqServerUrl)
|
||||||
|
.ReadFrom.Configuration(configuration)
|
||||||
|
.CreateLogger();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static IConfiguration GetConfiguration()
|
||||||
|
{
|
||||||
|
var builder = new ConfigurationBuilder()
|
||||||
|
.SetBasePath(Directory.GetCurrentDirectory())
|
||||||
|
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
|
||||||
|
.AddEnvironmentVariables();
|
||||||
|
|
||||||
|
return builder.Build();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -52,8 +52,8 @@ namespace Microsoft.eShopOnContainers.WebMVC
|
|||||||
{
|
{
|
||||||
JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear();
|
JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear();
|
||||||
|
|
||||||
loggerFactory.AddAzureWebAppDiagnostics();
|
//loggerFactory.AddAzureWebAppDiagnostics();
|
||||||
loggerFactory.AddApplicationInsights(app.ApplicationServices, LogLevel.Trace);
|
//loggerFactory.AddApplicationInsights(app.ApplicationServices, LogLevel.Trace);
|
||||||
|
|
||||||
app.UseHealthChecks("/hc", new HealthCheckOptions()
|
app.UseHealthChecks("/hc", new HealthCheckOptions()
|
||||||
{
|
{
|
||||||
|
@ -37,7 +37,10 @@
|
|||||||
<PackageReference Include="Microsoft.Web.LibraryManager.Build" Version="1.0.163" />
|
<PackageReference Include="Microsoft.Web.LibraryManager.Build" Version="1.0.163" />
|
||||||
<PackageReference Include="Microsoft.Build.Utilities.Core" Version="15.5.180" />
|
<PackageReference Include="Microsoft.Build.Utilities.Core" Version="15.5.180" />
|
||||||
<PackageReference Include="Serilog.AspNetCore" Version="2.1.1" />
|
<PackageReference Include="Serilog.AspNetCore" Version="2.1.1" />
|
||||||
|
<PackageReference Include="Serilog.Enrichers.Environment" Version="2.1.2" />
|
||||||
|
<PackageReference Include="Serilog.Settings.Configuration" Version="3.0.1" />
|
||||||
<PackageReference Include="Serilog.Sinks.Console" Version="3.1.1" />
|
<PackageReference Include="Serilog.Sinks.Console" Version="3.1.1" />
|
||||||
|
<PackageReference Include="Serilog.Sinks.Seq" Version="4.0.0" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<Target Name="PrepublishScript" BeforeTargets="PrepareForPublish">
|
<Target Name="PrepublishScript" BeforeTargets="PrepareForPublish">
|
||||||
|
@ -11,12 +11,15 @@
|
|||||||
"UseLoadTest": false,
|
"UseLoadTest": false,
|
||||||
"ActivateCampaignDetailFunction": "False",
|
"ActivateCampaignDetailFunction": "False",
|
||||||
"UseCustomizationData": false,
|
"UseCustomizationData": false,
|
||||||
"Logging": {
|
"Serilog": {
|
||||||
"IncludeScopes": false,
|
"SeqServerUrl": null,
|
||||||
"LogLevel": {
|
"MinimumLevel": {
|
||||||
"Default": "Trace",
|
"Default": "Information",
|
||||||
"System": "Information",
|
"Override": {
|
||||||
"Microsoft": "Information"
|
"Microsoft": "Warning",
|
||||||
|
"Microsoft.eShopOnContainers": "Information",
|
||||||
|
"System": "Warning"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"ApplicationInsights": {
|
"ApplicationInsights": {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user