{"id":25523990,"url":"https://github.com/jernejk/aspnetcoreserilogexample","last_synced_at":"2025-04-11T02:42:45.435Z","repository":{"id":41562234,"uuid":"190384107","full_name":"jernejk/AspNetCoreSerilogExample","owner":"jernejk","description":"This is an example of how to create an ASP .NET Core app with Serilog (.NET 5.0)","archived":false,"fork":false,"pushed_at":"2024-04-02T03:00:18.000Z","size":47,"stargazers_count":36,"open_issues_count":0,"forks_count":4,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-25T00:35:12.744Z","etag":null,"topics":["dotnet6","dotnetcore","logging","serilog"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jernejk.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2019-06-05T11:42:31.000Z","updated_at":"2024-06-09T08:45:03.000Z","dependencies_parsed_at":"2024-04-02T04:22:26.126Z","dependency_job_id":"9706e3b7-0d62-400e-812a-032b81b33cff","html_url":"https://github.com/jernejk/AspNetCoreSerilogExample","commit_stats":null,"previous_names":[],"tags_count":4,"template":true,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jernejk%2FAspNetCoreSerilogExample","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jernejk%2FAspNetCoreSerilogExample/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jernejk%2FAspNetCoreSerilogExample/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jernejk%2FAspNetCoreSerilogExample/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jernejk","download_url":"https://codeload.github.com/jernejk/AspNetCoreSerilogExample/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248331214,"owners_count":21085858,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["dotnet6","dotnetcore","logging","serilog"],"created_at":"2025-02-19T19:27:59.872Z","updated_at":"2025-04-11T02:42:45.412Z","avatar_url":"https://github.com/jernejk.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# ASP.NET Core 6.0 Serilog Template\n\nThis is an example of how to create a ASP .NET Core app with Serilog (.NET 7+)\n\nCheck my blog post for more details: [ASP.NET Core + Serilog](https://jkdev.me/asp-net-core-serilog/)\n\nIf you're looking for .NET (Core) versions, checkout the old branches: https://github.com/jernejk/AspNetCoreSerilogExample/branches\n\n## 1. Add Nuget packages\n\nIn `csproj` add:\n\n``` xml\n  \u003cItemGroup\u003e\n    \u003c!-- Serilog dependencies --\u003e\n    \u003cPackageReference Include=\"Serilog\" Version=\"2.9.0\" /\u003e\n    \u003cPackageReference Include=\"Serilog.AspNetCore\" Version=\"3.2.0\" /\u003e\n    \u003cPackageReference Include=\"Serilog.Enrichers.Environment\" Version=\"2.1.3\" /\u003e\n    \u003cPackageReference Include=\"Serilog.Exceptions\" Version=\"5.3.1\" /\u003e\n    \u003cPackageReference Include=\"Serilog.Extensions.Logging\" Version=\"3.0.1\" /\u003e\n    \u003cPackageReference Include=\"Serilog.Settings.Configuration\" Version=\"3.1.0\" /\u003e\n    \u003cPackageReference Include=\"Serilog.Sinks.Async\" Version=\"1.4.0\" /\u003e\n    \u003cPackageReference Include=\"Serilog.Sinks.Console\" Version=\"3.1.1\" /\u003e\n    \u003cPackageReference Include=\"Serilog.Sinks.RollingFile\" Version=\"3.3.0\" /\u003e\n    \u003cPackageReference Include=\"Serilog.Sinks.Seq\" Version=\"4.0.0\" /\u003e\n  \u003c/ItemGroup\u003e\n\n  \u003cItemGroup\u003e\n    \u003c!-- Make sure all of the necessary appsettings are included with the application. --\u003e\n    \u003cContent Update=\"appsettings*.json\"\u003e\n      \u003cCopyToOutputDirectory\u003ePreserveNewest\u003c/CopyToOutputDirectory\u003e\n      \u003cCopyToPublishDirectory\u003eAlways\u003c/CopyToPublishDirectory\u003e\n    \u003c/Content\u003e\n    \u003cContent Update=\"appsettings.Local.json\"\u003e\n      \u003cCopyToOutputDirectory\u003ePreserveNewest\u003c/CopyToOutputDirectory\u003e\n      \u003cCopyToPublishDirectory\u003eNever\u003c/CopyToPublishDirectory\u003e\n    \u003c/Content\u003e\n  \u003c/ItemGroup\u003e\n```\n\n## 2. Add Serilog configuration\n\nAdd Seq and async console configuration in `appsetings.json`:\n\n``` js\n  \"Serilog\": {\n    \"Using\": [ \"Serilog.Exceptions\", \"Serilog\", \"Serilog.Sinks.Console\", \"Serilog.Sinks.Seq\" ],\n    \"MinimumLevel\": {\n      \"Default\": \"Verbose\",\n      \"Override\": {\n        \"System\": \"Information\",\n        \"Microsoft\": \"Information\",\n        \"Microsoft.EntityFrameworkCore\": \"Information\"\n      }\n    },\n    \"WriteTo\": [\n      {\n        \"Name\": \"Seq\",\n        \"Args\": {\n          \"serverUrl\": \"http://localhost:5341\",\n          \"apiKey\": \"none\",\n          \"restrictedToMinimumLevel\": \"Verbose\"\n        }\n      },\n      {\n        \"Name\": \"Async\",\n        \"Args\": {\n          \"configure\": [\n            {\n              \"Name\": \"Console\",\n              \"Args\": {\n                \"restrictedToMinimumLevel\": \"Information\"\n              }\n            }\n          ]\n        }\n      }\n    ],\n    \"Enrich\": [ \"FromLogContext\", \"WithExceptionDetails\" ],\n    \"Properties\": {\n      \"Environment\": \"LocalDev\"\n    }\n  }\n```\n\n## 3. Update Program.cs (minimal APIs)\n\n``` cs\n// Logging based on https://github.com/jernejk/AspNetCoreSerilogExample and https://github.com/datalust/dotnet6-serilog-example\n// NOTE: When upgrading from .NET 5 or earlier, add `\u003cImplicitUsings\u003eenable\u003c/ImplicitUsings\u003e` to **.csproj** file under `\u003cPropertyGroup\u003e`.\n// NOTE: While you can still use full Program.cs and Startup.cs, `.UseSerilog()` is marked as obsolete for them. It's safer to move to minimal APIs.\nusing Serilog;\nusing System.Diagnostics;\n\ntry\n{\n    var builder = WebApplication.CreateBuilder(args);\n\n    builder.Host.UseSerilog((ctx, loggerConfiguration) =\u003e\n    {\n        loggerConfiguration\n            .ReadFrom.Configuration(ctx.Configuration)\n            .Enrich.FromLogContext()\n            .Enrich.WithProperty(\"ApplicationName\", typeof(Program).Assembly.GetName().Name)\n            .Enrich.WithProperty(\"Environment\", ctx.HostingEnvironment);\n\n#if DEBUG\n        // Used to filter out potentially bad data due debugging.\n        // Very useful when doing Seq dashboards and want to remove logs under debugging session.\n        loggerConfiguration.Enrich.WithProperty(\"DebuggerAttached\", Debugger.IsAttached);\n#endif\n    });\n\n    // Register services\n    builder.Services.AddControllers();\n    builder.Services.AddLogging();\n\n\n    WebApplication app = builder.Build();\n\n    // Rest of configuration.\n    if (app.Environment.IsDevelopment())\n    {\n        app.UseDeveloperExceptionPage();\n    }\n\n    // This will make the HTTP requests log as rich logs instead of plain text.\n    app.UseSerilogRequestLogging();\n\n    app.UseRouting();\n\n    // Absolute minimum setup, just return \"Hello world!\" to browser.\n    // You can use Controllers, SPA routing, SignalR, etc. routing.\n    app.UseEndpoints(endpoints =\u003e\n    {\n        endpoints.MapControllerRoute(name: \"default\", pattern: \"{controller}/{action=Index}/{id?}\");\n        endpoints.MapGet(\"\", context =\u003e context.Response.WriteAsync(\"Hello World!\\nUse /api/test/flatlog?input=Test, /api/test/StructuredLog?input=Test, etc. and observe console/Seq for logs.\"));\n    });\n\n    app.Run();\n}\ncatch (Exception ex)\n{\n    if (Log.Logger == null || Log.Logger.GetType().Name == \"SilentLogger\")\n    {\n        // Loading configuration or Serilog failed.\n        // This will create a logger that can be captured by Azure logger.\n        // To enable Azure logger, in Azure Portal:\n        // 1. Go to WebApp\n        // 2. App Service logs\n        // 3. Enable \"Application Logging (Filesystem)\", \"Application Logging (Filesystem)\" and \"Detailed error messages\"\n        // 4. Set Retention Period (Days) to 10 or similar value\n        // 5. Save settings\n        // 6. Under Overview, restart web app\n        // 7. Go to Log Stream and observe the logs\n        Log.Logger = new LoggerConfiguration()\n            .MinimumLevel.Debug()\n            .WriteTo.Console()\n            .CreateLogger();\n    }\n\n    Log.Fatal(ex, \"Host terminated unexpectedly\");\n}\nfinally\n{\n    Log.Information(\"Shut down complete\");\n    Log.CloseAndFlush();\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjernejk%2Faspnetcoreserilogexample","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjernejk%2Faspnetcoreserilogexample","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjernejk%2Faspnetcoreserilogexample/lists"}