From 95c89e981153b6886b789dabcd8f5f1fbd803267 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=91=D0=BE=D1=8F=D0=BA=D0=BE=D0=B2=20=D0=9C=D0=B8=D1=85?= =?UTF-8?q?=D0=B0=D0=B8=D0=BB=20=D0=92=D0=BB=D0=B0=D0=B4=D0=B8=D0=BC=D0=B8?= =?UTF-8?q?=D1=80=D0=BE=D0=B2=D0=B8=D1=87?= Date: Tue, 26 Nov 2024 15:52:10 +0500 Subject: [PATCH] feat: done --- Pages/Index.cshtml.cs | 3 +- Program.cs | 73 ++++++++++++++++++++++++++++-------- appsettings.Development.json | 10 +---- appsettings.json | 28 ++++++++++++-- telemetry.csproj | 8 ++++ 5 files changed, 92 insertions(+), 30 deletions(-) diff --git a/Pages/Index.cshtml.cs b/Pages/Index.cshtml.cs index 34a599f..706044b 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..1371c91 100644 --- a/Program.cs +++ b/Program.cs @@ -1,25 +1,66 @@ +using Elastic.Channels; +using Elastic.Ingest.Elasticsearch; +using Elastic.Ingest.Elasticsearch.DataStreams; +using Elastic.Serilog.Sinks; +using Elastic.Transport; +using Serilog; + 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")); // Basic Auth + }) + .Enrich.WithProperty("Environment", builder.Environment.EnvironmentName) + .ReadFrom.Configuration(builder.Configuration) + .CreateLogger(); -// Add services to the container. -builder.Services.AddRazorPages(); +try +{ + // Add services to the container. + builder.Services.AddRazorPages(); -var app = builder.Build(); + builder.Host.UseSerilog(); -// 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(); -} + builder.Services.AddSerilog((hostingContext, loggerConfiguration) => + loggerConfiguration.ReadFrom.Configuration(hostingContext.GetRequiredService())); -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.UseSerilogRequestLogging(); -app.MapRazorPages(); + app.UseRouting(); -app.Run(); + app.UseAuthorization(); + + app.MapRazorPages(); + + app.Run(); +} +catch (Exception e) +{ + Log.Logger.Error("{ex}", e); + 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..75bb6ba 100644 --- a/telemetry.csproj +++ b/telemetry.csproj @@ -4,6 +4,14 @@ net8.0 enable enable + 11 + + + + + + +