From b06c5eb68376c9dce2c1164c7a89f9bae6cab020 Mon Sep 17 00:00:00 2001 From: OK <143250301+OK219@users.noreply.github.com> Date: Tue, 11 Nov 2025 15:13:49 +0500 Subject: [PATCH 1/2] 1 --- Program.cs | 49 +++++++++++++++++++++++++----------- appsettings.Development.json | 9 ------- appsettings.json | 28 ++++++++++++++++++--- telemetry.csproj | 6 +++++ 4 files changed, 64 insertions(+), 28 deletions(-) delete mode 100644 appsettings.Development.json diff --git a/Program.cs b/Program.cs index bc275e4..9c3c87b 100644 --- a/Program.cs +++ b/Program.cs @@ -1,25 +1,44 @@ +using Serilog; + var builder = WebApplication.CreateBuilder(args); -// Add services to the container. -builder.Services.AddRazorPages(); +Log.Logger = new LoggerConfiguration() + .WriteTo.Console() + .WriteTo.File("logs/start-host-log-.txt", rollingInterval: RollingInterval.Day) + .CreateLogger(); +try +{ + builder.Services.AddRazorPages(); + + builder.Host.UseSerilog(); + + builder.Services.AddSerilog((hostingContext, loggerConfiguration) => + loggerConfiguration.ReadFrom.Configuration(builder.Configuration)); -var app = builder.Build(); + var app = builder.Build(); // 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(); -} + 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.UseHttpsRedirection(); -app.UseStaticFiles(); + app.UseHttpsRedirection(); + app.UseSerilogRequestLogging(); + app.UseStaticFiles(); -app.UseRouting(); + app.UseRouting(); -app.UseAuthorization(); + app.UseAuthorization(); -app.MapRazorPages(); + app.MapRazorPages(); -app.Run(); + app.Run(); +} +catch (Exception e) +{ + Console.WriteLine(e); + throw; +} \ No newline at end of file diff --git a/appsettings.Development.json b/appsettings.Development.json deleted file mode 100644 index 770d3e9..0000000 --- a/appsettings.Development.json +++ /dev/null @@ -1,9 +0,0 @@ -{ - "DetailedErrors": true, - "Logging": { - "LogLevel": { - "Default": "Information", - "Microsoft.AspNetCore": "Warning" - } - } -} 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..ee58e30 100644 --- a/telemetry.csproj +++ b/telemetry.csproj @@ -6,4 +6,10 @@ enable + + + + + + From c7371533904c6610af9aa5bbb2dd3755263a1dfb Mon Sep 17 00:00:00 2001 From: OK <143250301+OK219@users.noreply.github.com> Date: Tue, 11 Nov 2025 16:13:42 +0500 Subject: [PATCH 2/2] 234 --- Pages/Index.cshtml.cs | 3 ++- Program.cs | 29 ++++++++++++++++++++++++++--- telemetry.csproj | 1 + 3 files changed, 29 insertions(+), 4 deletions(-) diff --git a/Pages/Index.cshtml.cs b/Pages/Index.cshtml.cs index 34a599f..acf95d1 100644 --- a/Pages/Index.cshtml.cs +++ b/Pages/Index.cshtml.cs @@ -14,6 +14,7 @@ public IndexModel(ILogger logger) public void OnGet() { - + var myName = "OK"; + _logger.LogInformation("Sample log. My name is {MyName}", myName); } } diff --git a/Program.cs b/Program.cs index 9c3c87b..78158c0 100644 --- a/Program.cs +++ b/Program.cs @@ -1,3 +1,8 @@ +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); @@ -12,9 +17,27 @@ builder.Host.UseSerilog(); - builder.Services.AddSerilog((hostingContext, loggerConfiguration) => - loggerConfiguration.ReadFrom.Configuration(builder.Configuration)); - + builder.Services.AddSerilog((_, lc) => lc.Enrich.FromLogContext() + .WriteTo.Elasticsearch([new Uri("http://localhost:9200")], opts => + { + opts.DataStream = new DataStreamName("logs", "telemetry-loggin", "demo"); + opts.BootstrapMethod = BootstrapMethod.Failure; + opts.ConfigureChannel = channelOpts => + { + channelOpts.BufferOptions = new BufferOptions + { + ExportMaxConcurrency = 10 + }; + }; + }, transport => + { + transport.Authentication(new BasicAuthentication("elastic", "changeme")); // Basic Auth + // transport.Authentication(new ApiKey(base64EncodedApiKey)); // ApiKey + transport.OnRequestCompleted(d => Console.WriteLine($"es-req: {d.DebugInformation}")); + }) + .Enrich.WithProperty("Environment", builder.Environment.EnvironmentName) + .ReadFrom.Configuration(builder.Configuration)); + //builder.Services.AddSerilog(Log.Logger); var app = builder.Build(); // Configure the HTTP request pipeline. diff --git a/telemetry.csproj b/telemetry.csproj index ee58e30..884ab2c 100644 --- a/telemetry.csproj +++ b/telemetry.csproj @@ -7,6 +7,7 @@ +