diff --git a/.logs/log-20241126.txt b/.logs/log-20241126.txt new file mode 100644 index 0000000..f6d3d37 --- /dev/null +++ b/.logs/log-20241126.txt @@ -0,0 +1,11 @@ +{"@t":"2024-11-26T10:10:51.4792694Z","@m":"Failed to determine the https port for redirect.","@i":"ca76cc21","@l":"Warning","@tr":"ca458f7be1327abf8369f11c45bbf139","@sp":"8404fb473e9df816","EventId":{"Id":3,"Name":"FailedToDeterminePort"},"SourceContext":"Microsoft.AspNetCore.HttpsPolicy.HttpsRedirectionMiddleware","RequestId":"0HN8E0ACF4JPP:00000001","RequestPath":"/","ConnectionId":"0HN8E0ACF4JPP"} +{"@t":"2024-11-26T10:13:44.5991123Z","@m":"Failed to determine the https port for redirect.","@i":"ca76cc21","@l":"Warning","@tr":"e7704fa46ccd242094c2428644cdc72a","@sp":"bdc24de834dc0593","EventId":{"Id":3,"Name":"FailedToDeterminePort"},"SourceContext":"Microsoft.AspNetCore.HttpsPolicy.HttpsRedirectionMiddleware","RequestId":"0HN8E0C0SLE9V:00000001","RequestPath":"/","ConnectionId":"0HN8E0C0SLE9V"} +{"@t":"2024-11-26T10:19:58.1689197Z","@m":"Failed to determine the https port for redirect.","@i":"ca76cc21","@l":"Warning","@tr":"37925b06ba1ca8f43c5a9faff9f3f48b","@sp":"3a990b52653d72ba","EventId":{"Id":3,"Name":"FailedToDeterminePort"},"SourceContext":"Microsoft.AspNetCore.HttpsPolicy.HttpsRedirectionMiddleware","RequestId":"0HN8E0FG6TI6G:00000001","RequestPath":"/","ConnectionId":"0HN8E0FG6TI6G"} +{"@t":"2024-11-26T10:19:58.4985995Z","@m":"HTTP \"GET\" \"/\" responded 200 in 270.5689 ms","@i":"62d0885c","@tr":"37925b06ba1ca8f43c5a9faff9f3f48b","@sp":"3a990b52653d72ba","RequestMethod":"GET","RequestPath":"/","StatusCode":200,"Elapsed":270.5689,"SourceContext":"Serilog.AspNetCore.RequestLoggingMiddleware"} +{"@t":"2024-11-26T10:20:02.5880284Z","@m":"HTTP \"GET\" \"/\" responded 200 in 4.6351 ms","@i":"62d0885c","@tr":"22739e6f276966beb9ae36445c9e47c6","@sp":"1c61d7a67ca517a2","RequestMethod":"GET","RequestPath":"/","StatusCode":200,"Elapsed":4.6351,"SourceContext":"Serilog.AspNetCore.RequestLoggingMiddleware"} +{"@t":"2024-11-26T10:20:04.8871407Z","@m":"HTTP \"GET\" \"/Privacy\" responded 200 in 7.9765 ms","@i":"62d0885c","@tr":"3b3614f2bd86f1ccafcf3ad22265f5b6","@sp":"7696190144b0528b","RequestMethod":"GET","RequestPath":"/Privacy","StatusCode":200,"Elapsed":7.9765,"SourceContext":"Serilog.AspNetCore.RequestLoggingMiddleware"} +{"@t":"2024-11-26T10:20:07.1841667Z","@m":"HTTP \"GET\" \"/Privacy\" responded 200 in 1.7094 ms","@i":"62d0885c","@tr":"bf301bd11fccfcb11044ea834c29934f","@sp":"24c7b4d982b49cb2","RequestMethod":"GET","RequestPath":"/Privacy","StatusCode":200,"Elapsed":1.7094,"SourceContext":"Serilog.AspNetCore.RequestLoggingMiddleware"} +{"@t":"2024-11-26T10:34:01.2541855Z","@m":"Failed to determine the https port for redirect.","@i":"ca76cc21","@l":"Warning","@tr":"feaf4050833e028cfc3faebc77f1c136","@sp":"ffdec57388f59306","EventId":{"Id":3,"Name":"FailedToDeterminePort"},"SourceContext":"Microsoft.AspNetCore.HttpsPolicy.HttpsRedirectionMiddleware","RequestId":"0HN8E0NBFTILE:00000001","RequestPath":"/","ConnectionId":"0HN8E0NBFTILE"} +{"@t":"2024-11-26T10:34:01.5538242Z","@m":"HTTP \"GET\" \"/\" responded 200 in 229.0883 ms","@i":"62d0885c","@tr":"feaf4050833e028cfc3faebc77f1c136","@sp":"ffdec57388f59306","RequestMethod":"GET","RequestPath":"/","StatusCode":200,"Elapsed":229.0883,"SourceContext":"Serilog.AspNetCore.RequestLoggingMiddleware"} +{"@t":"2024-11-26T10:34:04.9477157Z","@m":"HTTP \"GET\" \"/Privacy\" responded 200 in 13.5567 ms","@i":"62d0885c","@tr":"84843dcebb3929db0c2e93a9c25f012c","@sp":"81a07ef334cebd9e","RequestMethod":"GET","RequestPath":"/Privacy","StatusCode":200,"Elapsed":13.5567,"SourceContext":"Serilog.AspNetCore.RequestLoggingMiddleware"} +{"@t":"2024-11-26T10:34:05.6937601Z","@m":"HTTP \"GET\" \"/\" responded 200 in 1.3388 ms","@i":"62d0885c","@tr":"ca7be3d7cf5faf97dd2eab03d6c2f5ce","@sp":"2ceb4fe31dad8f66","RequestMethod":"GET","RequestPath":"/","StatusCode":200,"Elapsed":1.3388,"SourceContext":"Serilog.AspNetCore.RequestLoggingMiddleware"} diff --git a/Pages/Index.cshtml.cs b/Pages/Index.cshtml.cs index 34a599f..3ba80b1 100644 --- a/Pages/Index.cshtml.cs +++ b/Pages/Index.cshtml.cs @@ -14,6 +14,7 @@ public IndexModel(ILogger logger) public void OnGet() { - + var myName = "Who am i?"; // ваше имя + _logger.LogInformation("Sample log. My name is {MyName}", myName); } } diff --git a/Program.cs b/Program.cs index bc275e4..2b6455f 100644 --- a/Program.cs +++ b/Program.cs @@ -1,25 +1,72 @@ -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(); -var app = builder.Build(); - -// Configure the HTTP request pipeline. -if (!app.Environment.IsDevelopment()) +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); + + // Add services to the container. + 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); + + builder.Services.AddRazorPages(); -app.UseHttpsRedirection(); -app.UseStaticFiles(); -app.UseRouting(); -app.UseAuthorization(); + var app = builder.Build(); -app.MapRazorPages(); + // 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.Run(); + app.UseHttpsRedirection(); + app.UseStaticFiles(); + app.UseSerilogRequestLogging(); + app.UseRouting(); + + app.UseAuthorization(); + + app.MapRazorPages(); + + app.Run(); + +} +catch(Exception e) +{ + Log.Error(e.Message); + throw; +} 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 + + + + + + +