Last active
September 8, 2021 06:31
-
-
Save Alger23/bb848e902a5347d072d9853f79475159 to your computer and use it in GitHub Desktop.
Serilog Add environment variable value
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
{ | |
"Logging": { | |
"LogLevel": { | |
"Default": "Debug", | |
"Microsoft": "Warning", | |
"Microsoft.Hosting.Lifetime": "Information" | |
} | |
}, | |
"Serilog": { | |
"LevelSwitches": { | |
"$controlSwitch": "Debug" | |
}, | |
"WriteTo": [ | |
{ | |
"Name": "Seq", | |
"Args": { | |
"serverUrl": "http://localhost:5431", | |
"apiKey": "<Your_Seq_API_Key>", | |
"controlLevelSwitch": "$controlSwitch" | |
} | |
}, | |
{ | |
"Name": "Elasticsearch", | |
"Args": { | |
"nodeUris": "http://localhost:9200;http://remote-pc:9200", | |
"indexFormat": "custom-index-{0:yyyy-MM}", | |
// "templateName": "myCustomTemplate", | |
// "typeName": "myCustomLogEventType", | |
// "pipelineName": "myCustomPipelineName", | |
"batchPostingLimit": 50, | |
"batchAction": "Create", | |
"period": 2, | |
"inlineFields": true, | |
"restrictedToMinimumLevel": "Debug", | |
// "bufferBaseFilename": "C:/Temp/docker-elk-serilog-web-buffer", | |
// "bufferFileSizeLimitBytes": 5242880, | |
// "bufferLogShippingInterval": 5000, | |
// "bufferRetainedInvalidPayloadsLimitBytes": 5000, | |
// "bufferFileCountLimit": 31, | |
// "connectionGlobalHeaders" :"Authorization=Bearer SOME-TOKEN;OtherHeader=OTHER-HEADER-VALUE", | |
// "connectionTimeout": 5, | |
// "emitEventFailure": "WriteToSelfLog", | |
// "queueSizeLimit": "100000", | |
"autoRegisterTemplate": true, | |
"autoRegisterTemplateVersion": "ESv2", | |
// "overwriteTemplate": false, | |
"registerTemplateFailure": "IndexAnyway", | |
"deadLetterIndexName": "deadletter-{0:yyyy-MM}", | |
// "numberOfShards: 20, | |
// "numberOfReplicas": 10, | |
// "templateCustomSettings": [{ "index.mapping.total_fields.limit": "10000000" } ], | |
// "formatProvider": "My.Namespace.MyFormatProvider, My.Assembly.Name", | |
// "connection": "My.Namespace.MyConnection, My.Assembly.Name", | |
// "serializer": "My.Namespace.MySerializer, My.Assembly.Name", | |
// "connectionPool": "My.Namespace.MyConnectionPool, My.Assembly.Name", | |
// "customFormatter": "My.Namespace.MyCustomFormatter, My.Assembly.Name", | |
// "customDurableFormatter": "My.Namespace.MyCustomDurableFormatter, My.Assembly.Name", | |
// "failureSink": "My.Namespace.MyFailureSink, My.Assembly.Name" | |
} | |
}, | |
{ | |
"Name": "Console", | |
"Args": { | |
"outputTemplate": "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj} <s:{SourceContext}>{NewLine}{Exception}", | |
"controlLevelSwitch": "$controlSwitch" | |
} | |
} | |
] | |
} | |
} |
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 System; | |
using System.Reflection; | |
using Microsoft.AspNetCore.Hosting; | |
using Microsoft.Extensions.Hosting; | |
using Serilog; | |
using Serilog.Events; | |
using Serilog.Exceptions; | |
/* | |
<PackageReference Include="Serilog.Enrichers.Environment" Version="2.2.0" /> | |
<PackageReference Include="Serilog.Exceptions" Version="7.0.0" /> | |
<PackageReference Include="Serilog.AspNetCore" Version="4.0.0" /> | |
<PackageReference Include="Serilog.Sinks.Seq" Version="5.0.0" /> | |
or | |
<PackageReference Include="Serilog.Sinks.Elasticsearch" Version="8.4.1" /> | |
*/ | |
namespace SerilogInWebApiSample | |
{ | |
public class Program | |
{ | |
public static int Main(string[] args) | |
{ | |
Log.Logger = new LoggerConfiguration() | |
.MinimumLevel.Override("Microsoft", LogEventLevel.Information) | |
.Enrich.FromLogContext() | |
.WriteTo.Console() | |
.CreateBootstrapLogger(); | |
try | |
{ | |
Log.Information("Starting web host"); | |
CreateHostBuilder(args).Build().Run(); | |
return 0; | |
} | |
catch (Exception ex) | |
{ | |
Log.Fatal($"Failed to start {Assembly.GetExecutingAssembly().GetName().Name}, Host terminated unexpectedly.", ex); | |
return 1; | |
} | |
finally | |
{ | |
Log.CloseAndFlush(); | |
} | |
} | |
public static IHostBuilder CreateHostBuilder(string[] args) => | |
Host.CreateDefaultBuilder(args) | |
.UseSerilog((context, services, configuration) => | |
{ | |
var imageVersion = Assembly.GetAssembly(typeof(Program)) | |
.GetCustomAttribute<AssemblyInformationalVersionAttribute>() | |
.InformationalVersion; | |
//var environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT"); | |
configuration | |
.ReadFrom.Configuration(context.Configuration) | |
.ReadFrom.Services(services) | |
.Enrich.WithMachineName() // in Serilog.Enrichers.Environment | |
.Enrich.WithEnvironmentName() // in Serilog.Enrichers.Environment | |
.Enrich.WithProperty("Application", context.HostingEnvironment.ApplicationName) | |
//.Enrich.WithProperty("Environment", context.HostingEnvironment.EnvironmentName) | |
//.Enrich.WithProperty("environment_env", environment) | |
.Enrich.WithProperty("Version", imageVersion) | |
.Enrich.FromLogContext() | |
.Enrich.WithExceptionDetails(); // in Serilog.Exceptions | |
}) | |
.ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); }); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment