From edb0a8ba918e62029a2838398f40c46ee7025b4c Mon Sep 17 00:00:00 2001 From: Khalikov Idris Date: Tue, 26 Nov 2024 16:26:25 +0500 Subject: [PATCH 1/2] task completed --- Pages/Index.cshtml.cs | 4 +- Program.cs | 79 ++++++++++++++++++++++++++++-------- appsettings.Development.json | 10 +---- appsettings.json | 28 +++++++++++-- telemetry.csproj | 7 ++++ 5 files changed, 96 insertions(+), 32 deletions(-) diff --git a/Pages/Index.cshtml.cs b/Pages/Index.cshtml.cs index 34a599f..f81bd58 100644 --- a/Pages/Index.cshtml.cs +++ b/Pages/Index.cshtml.cs @@ -1,4 +1,3 @@ -using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.RazorPages; namespace telemetry.Pages; @@ -14,6 +13,7 @@ public IndexModel(ILogger logger) public void OnGet() { - + var myName = "Idris Khalikov and Vladimir Obraztsov"; + _logger.LogInformation("Sample log. My name is {MyName}", myName); } } diff --git a/Program.cs b/Program.cs index bc275e4..8860f2f 100644 --- a/Program.cs +++ b/Program.cs @@ -1,25 +1,70 @@ -var builder = WebApplication.CreateBuilder(args); +using Elastic.Channels; +using Elastic.Ingest.Elasticsearch; +using Elastic.Ingest.Elasticsearch.DataStreams; +using Elastic.Serilog.Sinks; +using Elastic.Transport; +using Serilog; -// Add services to the container. -builder.Services.AddRazorPages(); +try +{ + var builder = WebApplication.CreateBuilder(args); -var app = builder.Build(); + // Add services to the container. + builder.Services.AddRazorPages(); -// Configure the HTTP request pipeline. -if (!app.Environment.IsDevelopment()) -{ - 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(); -} + Log.Logger = new LoggerConfiguration() + .Enrich.FromLogContext() + .WriteTo.Elasticsearch([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")); // Basic Auth + }) + .Enrich.WithProperty("Environment", builder.Environment.EnvironmentName) + .ReadFrom.Configuration(builder.Configuration) + .CreateLogger(); + + + builder.Host.UseSerilog(); + + builder.Services.AddSerilog(Log.Logger); -app.UseHttpsRedirection(); -app.UseStaticFiles(); + var app = builder.Build(); -app.UseRouting(); + // Configure the HTTP request pipeline. + if (!app.Environment.IsDevelopment()) + { + 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(); + } -app.UseAuthorization(); + app.UseHttpsRedirection(); + app.UseStaticFiles(); -app.MapRazorPages(); + app.UseSerilogRequestLogging(); -app.Run(); + app.UseRouting(); + + app.UseAuthorization(); + + app.MapRazorPages(); + + app.Run(); +} +catch (Exception ex) +{ + Log.Logger = new LoggerConfiguration().WriteTo + .File(".logs/start-host-log-.txt", rollingInterval: RollingInterval.Day).CreateLogger(); + Log.Logger.Fatal(ex.Message); + throw; +} \ No newline at end of file diff --git a/appsettings.Development.json b/appsettings.Development.json index 770d3e9..9e26dfe 100644 --- a/appsettings.Development.json +++ b/appsettings.Development.json @@ -1,9 +1 @@ -{ - "DetailedErrors": true, - "Logging": { - "LogLevel": { - "Default": "Information", - "Microsoft.AspNetCore": "Warning" - } - } -} +{} \ No newline at end of file diff --git a/appsettings.json b/appsettings.json index 10f68b8..6f5ed00 100644 --- a/appsettings.json +++ b/appsettings.json @@ -1,9 +1,29 @@ { - "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": "*" -} +} \ No newline at end of file diff --git a/telemetry.csproj b/telemetry.csproj index 1b28a01..ddd14dd 100644 --- a/telemetry.csproj +++ b/telemetry.csproj @@ -6,4 +6,11 @@ enable + + + + + + + From ff58281b7951293c758c379abebfbb65ed4a9276 Mon Sep 17 00:00:00 2001 From: Khalikov Idris Date: Tue, 26 Nov 2024 17:27:15 +0500 Subject: [PATCH 2/2] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB?= =?UTF-8?q?=20bootstrap=20=D0=BB=D0=BE=D0=B3=D0=B3=D0=B5=D1=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Program.cs | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/Program.cs b/Program.cs index 8860f2f..c79388f 100644 --- a/Program.cs +++ b/Program.cs @@ -5,6 +5,12 @@ using Elastic.Transport; using Serilog; + +Log.Logger = new LoggerConfiguration() + .WriteTo.File(".logs/start-host-log-.txt", rollingInterval: RollingInterval.Day) + .WriteTo.Console() + .CreateBootstrapLogger(); + try { var builder = WebApplication.CreateBuilder(args); @@ -12,7 +18,10 @@ // Add services to the container. builder.Services.AddRazorPages(); - Log.Logger = new LoggerConfiguration() + + builder.Host.UseSerilog(); + + builder.Services.AddSerilog((services, loggerConfiguration) => loggerConfiguration .Enrich.FromLogContext() .WriteTo.Elasticsearch([new Uri("http://localhost:9200")], opts => { @@ -30,13 +39,7 @@ transport.Authentication(new BasicAuthentication("elastic", "changeme")); // Basic Auth }) .Enrich.WithProperty("Environment", builder.Environment.EnvironmentName) - .ReadFrom.Configuration(builder.Configuration) - .CreateLogger(); - - - builder.Host.UseSerilog(); - - builder.Services.AddSerilog(Log.Logger); + .ReadFrom.Configuration(builder.Configuration)); var app = builder.Build(); @@ -63,8 +66,6 @@ } catch (Exception ex) { - Log.Logger = new LoggerConfiguration().WriteTo - .File(".logs/start-host-log-.txt", rollingInterval: RollingInterval.Day).CreateLogger(); - Log.Logger.Fatal(ex.Message); + Log.Logger.Fatal(ex, "Unhandled exception"); throw; } \ No newline at end of file