diff --git a/Pages/Index.cshtml.cs b/Pages/Index.cshtml.cs index 34a599f..8f2199e 100644 --- a/Pages/Index.cshtml.cs +++ b/Pages/Index.cshtml.cs @@ -14,6 +14,7 @@ public IndexModel(ILogger logger) public void OnGet() { - + var myName = "..."; + _logger.LogInformation("Sample log. My name is {MyName}", myName); } } diff --git a/Program.cs b/Program.cs index bc275e4..b1ebbd4 100644 --- a/Program.cs +++ b/Program.cs @@ -1,25 +1,45 @@ -var builder = WebApplication.CreateBuilder(args); - -// Add services to the container. -builder.Services.AddRazorPages(); - -var app = builder.Build(); - -// Configure the HTTP request pipeline. -if (!app.Environment.IsDevelopment()) +using Serilog; +using Elastic.Channels; +using Elastic.Ingest.Elasticsearch; +using Elastic.Ingest.Elasticsearch.DataStreams; +using Elastic.Serilog.Sinks; +using Elastic.Transport; + +try { - app.UseExceptionHandler("/Error"); - // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts. - app.UseHsts(); + var builder = WebApplication.CreateBuilder(args); + Log.Logger = new LoggerConfiguration() + .Enrich.FromLogContext() + .WriteTo.Elasticsearch(new[] { new Uri("http://localhost:9200") }, opts => + { + opts.DataStream = new DataStreamName("logs", "telemetry-logging", "demo"); + opts.BootstrapMethod = BootstrapMethod.Failure; + opts.ConfigureChannel = channelOpts => + { + channelOpts.BufferOptions = new BufferOptions { ExportMaxConcurrency = 10 }; + }; + }, transport => { transport.Authentication(new BasicAuthentication("elastic", "changeme")); }) + .Enrich.WithProperty("Environment", builder.Environment.EnvironmentName) + .ReadFrom.Configuration(builder.Configuration) + .CreateLogger(); + builder.Host.UseSerilog(); + builder.Services.AddSerilog(Log.Logger); + builder.Services.AddRazorPages(); + var app = builder.Build(); + if (!app.Environment.IsDevelopment()) + { + app.UseExceptionHandler("/Error"); + app.UseHsts(); + } + app.UseHttpsRedirection(); + app.UseStaticFiles(); + app.UseRouting(); + app.UseAuthorization(); + app.MapRazorPages(); + app.Run(); } - -app.UseHttpsRedirection(); -app.UseStaticFiles(); - -app.UseRouting(); - -app.UseAuthorization(); - -app.MapRazorPages(); - -app.Run(); +catch (Exception e) +{ + Log.Error(e.Message); + throw; +} \ No newline at end of file diff --git a/appsettings.Development.json b/appsettings.Development.json index 770d3e9..0967ef4 100644 --- a/appsettings.Development.json +++ b/appsettings.Development.json @@ -1,9 +1 @@ -{ - "DetailedErrors": true, - "Logging": { - "LogLevel": { - "Default": "Information", - "Microsoft.AspNetCore": "Warning" - } - } -} +{} diff --git a/appsettings.json b/appsettings.json index 10f68b8..ba3e2cd 100644 --- a/appsettings.json +++ b/appsettings.json @@ -1,8 +1,28 @@ { - "Logging": { - "LogLevel": { + "Serilog": { + "WriteTo": [ + { + "Name": "File", + "Args": { + "path": ".logs/log-.txt", + "formatter": "Serilog.Formatting.Compact.RenderedCompactJsonFormatter, Serilog.Formatting.Compact", + "rollingInterval": "Day", + "rollOnFileSizeLimit": true + } + }, + { + "Name": "Console", + "Args": { + "outputTemplate": "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj} {Properties:j}{NewLine}{Exception}" + } + } + ], + "MinimumLevel": { "Default": "Information", - "Microsoft.AspNetCore": "Warning" + "Override": { + "Microsoft": "Warning", + "System": "Warning" + } } }, "AllowedHosts": "*" diff --git a/telemetry.csproj b/telemetry.csproj index 1b28a01..a3f7d9d 100644 --- a/telemetry.csproj +++ b/telemetry.csproj @@ -6,4 +6,11 @@ enable + + + + + + +