Created
October 26, 2022 09:56
-
-
Save OFark/3243b8bb56d866efad73b062a64fb584 to your computer and use it in GitHub Desktop.
Adding Serilog and Application Insights to .Net core WebApp
This file contains 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
public sealed record ApplicationInsightsOptions : ILoggerOptions | |
{ | |
public bool Enabled { get; init; } | |
public string? InstrumentationKey { get; init; } | |
} |
This file contains 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": { | |
"WriteTo": { | |
"ElasticSearch": { | |
"Enabled": true, | |
"Uri": "http://localhost:9200" | |
}, | |
"ApplicationInsights": { | |
"Enabled": false | |
} | |
} | |
}, | |
... | |
} |
This file contains 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
public sealed record ElasticSearchOptions : ILoggerOptions | |
{ | |
public bool Enabled { get; init; } | |
public string? Uri { get; init; } | |
} |
This file contains 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
public interface ILoggerOptions | |
{ | |
public bool Enabled { get; } | |
} |
This file contains 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.ApplicationInsights.Extensibility; | |
using Microsoft.Extensions.Logging; | |
using Presentation.Logging; | |
using Serilog.Sinks.Elasticsearch; | |
using System.Configuration; | |
namespace Presentation.AppSettings; | |
static internal class LoggingSetup | |
{ | |
static internal WebApplicationBuilder ConfigureLogging(this WebApplicationBuilder builder, out ILogger<Program> logger) | |
{ | |
Serilog.Debugging.SelfLog.Enable(msg => Console.WriteLine(msg)); | |
builder.Host.UseSerilog((context, services, config) => | |
{ | |
config.Enrich.FromLogContext() | |
.Enrich.WithMachineName() | |
.WriteTo.Console() | |
.Enrich.WithProperty("Environment", context.HostingEnvironment.EnvironmentName ?? "Unknown") | |
.ReadFrom.Configuration(builder.Configuration); | |
var elasticSearchOptions = builder.Configuration.GetSection("Logging:WriteTo:ElasticSearch").Get<ElasticSearchOptions>(); | |
var applicationInsightsOptions = builder.Configuration.GetSection("Logging:WriteTo:ApplicationInsights").Get<ApplicationInsightsOptions>(); | |
if (elasticSearchOptions is ElasticSearchOptions eso && eso.Enabled) | |
{ | |
config.WriteTo.Elasticsearch(new ElasticsearchSinkOptions(new Uri(eso.Uri ?? throw new ConfigurationErrorsException("Elasticsearch Configuration Uri is missing"))) | |
{ | |
IndexFormat = $"{builder.Configuration["ApplicationSettings:Name"]}-logs-{context.HostingEnvironment.EnvironmentName?.ToLower().Replace(".", "-")}-{DateTime.UtcNow:yyyy-MM}", | |
DetectElasticsearchVersion = true, | |
RegisterTemplateFailure = RegisterTemplateRecovery.IndexAnyway, | |
NumberOfShards = 2, | |
NumberOfReplicas = 0, | |
FailureCallback = e => Console.WriteLine($"Serilog: Unable to submit event {e.MessageTemplate}"), | |
EmitEventFailure = EmitEventFailureHandling.WriteToSelfLog | | |
EmitEventFailureHandling.WriteToFailureSink | | |
EmitEventFailureHandling.RaiseCallback | |
}); | |
} | |
if (applicationInsightsOptions is ApplicationInsightsOptions aio && aio.Enabled) | |
{ | |
config.WriteTo.ApplicationInsights(services.GetRequiredService<TelemetryConfiguration>(), TelemetryConverter.Events); | |
} | |
}); | |
builder.Logging.AddApplicationInsights(); | |
builder.Logging.AddEventSourceLogger(); | |
builder.Services.AddApplicationInsightsTelemetry(); | |
using var loggerFactory = LoggerFactory.Create(builder => builder.AddSerilog()); | |
logger = loggerFactory.CreateLogger<Program>(); | |
return builder; | |
} | |
} |
This file contains 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
var builder = WebApplication.CreateBuilder(args); | |
builder.ConfigureLogging(out var logger) | |
... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment