From 26fbf48a85e1f51d2a6e4831d463beaa72c47a09 Mon Sep 17 00:00:00 2001 From: senyapekus Date: Sun, 1 Dec 2024 19:07:01 +0500 Subject: [PATCH] done --- .logs/log-20241201.txt | 8 ++++ Pages/Index.cshtml.cs | 3 +- Program.cs | 83 ++++++++++++++++++++++++++---------- appsettings.Development.json | 10 +---- appsettings.json | 26 +++++++++-- telemetry.csproj | 7 +++ 6 files changed, 101 insertions(+), 36 deletions(-) create mode 100644 .logs/log-20241201.txt diff --git a/.logs/log-20241201.txt b/.logs/log-20241201.txt new file mode 100644 index 0000000..e22e519 --- /dev/null +++ b/.logs/log-20241201.txt @@ -0,0 +1,8 @@ +{"@t":"2024-12-01T14:02:49.9144101Z","@m":"Failed to determine the https port for redirect.","@i":"ca76cc21","@l":"Warning","@tr":"ffc049e44e3f1bdfa6d3bbf9eaea3602","@sp":"2ae0706c7ac125e3","EventId":{"Id":3,"Name":"FailedToDeterminePort"},"SourceContext":"Microsoft.AspNetCore.HttpsPolicy.HttpsRedirectionMiddleware","RequestId":"0HN8I23BM6I9D:00000001","RequestPath":"/","ConnectionId":"0HN8I23BM6I9D","Environment":"Development"} +{"@t":"2024-12-01T14:02:50.0187580Z","@m":"Sample log. My name is \"vashe name\"","@i":"04820aad","@tr":"ffc049e44e3f1bdfa6d3bbf9eaea3602","@sp":"2ae0706c7ac125e3","MyName":"vashe name","SourceContext":"telemetry.Pages.IndexModel","ActionId":"de03b54a-3712-444b-9b1e-c7f822c68c36","ActionName":"/Index","RequestId":"0HN8I23BM6I9D:00000001","RequestPath":"/","ConnectionId":"0HN8I23BM6I9D","Environment":"Development"} +{"@t":"2024-12-01T14:02:50.1037174Z","@m":"HTTP \"GET\" \"/\" responded 200 in 118.9082 ms","@i":"62d0885c","@tr":"ffc049e44e3f1bdfa6d3bbf9eaea3602","@sp":"2ae0706c7ac125e3","RequestMethod":"GET","RequestPath":"/","StatusCode":200,"Elapsed":118.9082,"SourceContext":"Serilog.AspNetCore.RequestLoggingMiddleware","RequestId":"0HN8I23BM6I9D:00000001","ConnectionId":"0HN8I23BM6I9D","Environment":"Development"} +{"@t":"2024-12-01T14:02:59.5621578Z","@m":"Sample log. My name is \"vashe name\"","@i":"04820aad","@tr":"c064a28539a2b7abca3efef0845bad5e","@sp":"f135390a316b6395","MyName":"vashe name","SourceContext":"telemetry.Pages.IndexModel","ActionId":"de03b54a-3712-444b-9b1e-c7f822c68c36","ActionName":"/Index","RequestId":"0HN8I23BM6I9D:00000002","RequestPath":"/","ConnectionId":"0HN8I23BM6I9D","Environment":"Development"} +{"@t":"2024-12-01T14:02:59.5669133Z","@m":"HTTP \"GET\" \"/\" responded 200 in 5.1232 ms","@i":"62d0885c","@tr":"c064a28539a2b7abca3efef0845bad5e","@sp":"f135390a316b6395","RequestMethod":"GET","RequestPath":"/","StatusCode":200,"Elapsed":5.1232,"SourceContext":"Serilog.AspNetCore.RequestLoggingMiddleware","RequestId":"0HN8I23BM6I9D:00000002","ConnectionId":"0HN8I23BM6I9D","Environment":"Development"} +{"@t":"2024-12-01T14:03:00.4554363Z","@m":"HTTP \"GET\" \"/Privacy\" responded 200 in 5.2466 ms","@i":"62d0885c","@tr":"6612c07a7351dd13dd647ef3e2c75ba7","@sp":"4eea1f12572d85d4","RequestMethod":"GET","RequestPath":"/Privacy","StatusCode":200,"Elapsed":5.2466,"SourceContext":"Serilog.AspNetCore.RequestLoggingMiddleware","RequestId":"0HN8I23BM6I9D:00000003","ConnectionId":"0HN8I23BM6I9D","Environment":"Development"} +{"@t":"2024-12-01T14:03:04.1853687Z","@m":"Sample log. My name is \"vashe name\"","@i":"04820aad","@tr":"03a43c5df01351f20dd253d8894f01bf","@sp":"cc60151de469bb1b","MyName":"vashe name","SourceContext":"telemetry.Pages.IndexModel","ActionId":"de03b54a-3712-444b-9b1e-c7f822c68c36","ActionName":"/Index","RequestId":"0HN8I23BM6I9D:00000004","RequestPath":"/","ConnectionId":"0HN8I23BM6I9D","Environment":"Development"} +{"@t":"2024-12-01T14:03:04.1873547Z","@m":"HTTP \"GET\" \"/\" responded 200 in 2.1625 ms","@i":"62d0885c","@tr":"03a43c5df01351f20dd253d8894f01bf","@sp":"cc60151de469bb1b","RequestMethod":"GET","RequestPath":"/","StatusCode":200,"Elapsed":2.1625,"SourceContext":"Serilog.AspNetCore.RequestLoggingMiddleware","RequestId":"0HN8I23BM6I9D:00000004","ConnectionId":"0HN8I23BM6I9D","Environment":"Development"} diff --git a/Pages/Index.cshtml.cs b/Pages/Index.cshtml.cs index 34a599f..faec875 100644 --- a/Pages/Index.cshtml.cs +++ b/Pages/Index.cshtml.cs @@ -14,6 +14,7 @@ public IndexModel(ILogger logger) public void OnGet() { - + var myName = "vashe name"; // ваше имя + _logger.LogInformation("Sample log. My name is {MyName}", myName); } } diff --git a/Program.cs b/Program.cs index bc275e4..79e1a9c 100644 --- a/Program.cs +++ b/Program.cs @@ -1,25 +1,62 @@ -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 Elastic.Channels; +using Elastic.Ingest.Elasticsearch; +using Elastic.Ingest.Elasticsearch.DataStreams; +using Elastic.Serilog.Sinks; +using Elastic.Transport; +using Serilog; + +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")); // Basic Auth + }) + .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"); + // 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.UseSerilogRequestLogging(); + app.UseRouting(); + + app.UseAuthorization(); + + app.MapRazorPages(); + + app.Run(); +} +catch (Exception ex) +{ + Log.Logger.Error(ex.ToString()); } - -app.UseHttpsRedirection(); -app.UseStaticFiles(); - -app.UseRouting(); - -app.UseAuthorization(); - -app.MapRazorPages(); - -app.Run(); 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..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 + + + + + + +