diff --git a/DotnetFoundation/DotnetFoundation.Api/DotnetFoundation.Api.csproj b/DotnetFoundation/DotnetFoundation.Api/DotnetFoundation.Api.csproj index 6285449..ad078ef 100644 --- a/DotnetFoundation/DotnetFoundation.Api/DotnetFoundation.Api.csproj +++ b/DotnetFoundation/DotnetFoundation.Api/DotnetFoundation.Api.csproj @@ -8,6 +8,9 @@ + + + diff --git a/DotnetFoundation/DotnetFoundation.Api/Program.cs b/DotnetFoundation/DotnetFoundation.Api/Program.cs index 118237c..8a99f72 100644 --- a/DotnetFoundation/DotnetFoundation.Api/Program.cs +++ b/DotnetFoundation/DotnetFoundation.Api/Program.cs @@ -4,6 +4,8 @@ using DotnetFoundation.Services; using Microsoft.AspNetCore.Mvc; using Microsoft.OpenApi.Models; +using Serilog; +using Serilog.Templates; using Swashbuckle.AspNetCore.Filters; using System.Reflection; using System.Text.Json.Serialization; @@ -52,6 +54,21 @@ options.IncludeXmlComments(filePath); }); +// logging using serilog +Log.Logger = new LoggerConfiguration() + .WriteTo.File( + formatter: new ExpressionTemplate( + "{ {Timestamp: @t, Level: @l, TracebackId: @tr, RequestMethod, RequestPath, StatusCode, SourceContext, Data:{EventId: @i, RequestId}, Message: @m, Exception: @x} }\n\n" + ), + path: "logs/log-.ndjson", + rollingInterval: RollingInterval.Day) + .WriteTo.Console( + outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss zzz} [{Level:u3}] {Message:lj} {NewLine}{Exception}\n") + .CreateLogger(); + +Log.Logger.Information("Logging has started"); +builder.Host.UseSerilog(); + // Adding HTTP Context builder.Services.AddHttpContextAccessor(); @@ -74,6 +91,8 @@ app.UseHttpsRedirection(); +app.UseSerilogRequestLogging(); + app.UseAuthentication(); app.UseAuthorization(); diff --git a/DotnetFoundation/DotnetFoundation.Api/appsettings.Development.json b/DotnetFoundation/DotnetFoundation.Api/appsettings.Development.json index 0c208ae..1b2d3ba 100644 --- a/DotnetFoundation/DotnetFoundation.Api/appsettings.Development.json +++ b/DotnetFoundation/DotnetFoundation.Api/appsettings.Development.json @@ -5,4 +5,4 @@ "Microsoft.AspNetCore": "Warning" } } -} +} \ No newline at end of file