{"id":5516,"date":"2021-11-05T10:02:08","date_gmt":"2021-11-05T09:02:08","guid":{"rendered":"https:\/\/solidt.eu\/site\/?p=5516"},"modified":"2021-11-05T10:02:08","modified_gmt":"2021-11-05T09:02:08","slug":"dotnet-api-custom-logging","status":"publish","type":"post","link":"https:\/\/solidt.eu\/site\/dotnet-api-custom-logging\/","title":{"rendered":"DotNet API Custom Logging"},"content":{"rendered":"\n<div style=\"height: 250px; position:relative; margin-bottom: 50px;\" class=\"wp-block-simple-code-block-ace\"><pre class=\"wp-block-simple-code-block-ace\" style=\"position:absolute;top:0;right:0;bottom:0;left:0\" data-mode=\"csharp\" data-theme=\"monokai\" data-fontsize=\"14\" data-lines=\"Infinity\" data-showlines=\"true\" data-copy=\"false\">\/\/using System;\n\/\/using Domain;\n\/\/using Microsoft.AspNetCore;\n\/\/using Microsoft.AspNetCore.Hosting;\n\/\/using Microsoft.Extensions.Logging;\n\npublic static IWebHost BuildWebHost(string[] args) =>\n\t\tWebHost.CreateDefaultBuilder(args)\n\t\t\t.ConfigureLogging((ILoggingBuilder logging) =>\n\t\t\t{\n\t\t\t\tlogging.ClearProviders();\n\t\t\t\t\/\/logging.AddConsole();\n\t\t\t\tlogging.AddProvider(new CustomLoggerProvider());\n\t\t\t})\n\t\t\t.UseStartup&lt;Startup>()\n\t\t\t.Build();\n}\n\npublic class CustomLogger : ILogger\n{\n\tpublic IDisposable BeginScope&lt;TState>(TState state) => default;\n\tpublic bool IsEnabled(LogLevel logLevel) => true;\n\n\tpublic void Log&lt;TState>(\n\t\tLogLevel logLevel,\n\t\tEventId eventId,\n\t\tTState state,\n\t\tException exception,\n\t\tFunc&lt;TState, Exception, string> formatter)\n\t{\n\t\tif (!IsEnabled(logLevel)) return;\n\t\tDomainPorts.Logger.Log(ConvertLogLevel(logLevel), formatter(state, exception), exception);\n\t}\n\n\tprivate static DNA.Shared.Logging.LogLevel ConvertLogLevel(LogLevel logLevel)\n\t{\n\t\tswitch (logLevel)\n\t\t{\n\t\t\tcase LogLevel.Trace: return DNA.Shared.Logging.LogLevel.Debug;\n\t\t\tcase LogLevel.Debug: return DNA.Shared.Logging.LogLevel.Debug;\n\t\t\tcase LogLevel.Information:\n\t\t\t\treturn DNA.Shared.Logging.LogLevel.Info;\n\t\t\tcase LogLevel.Warning:\n\t\t\t\treturn DNA.Shared.Logging.LogLevel.Warning;\n\t\t\tcase LogLevel.Error:\n\t\t\t\treturn DNA.Shared.Logging.LogLevel.Error;\n\t\t\tcase LogLevel.Critical:\n\t\t\t\treturn DNA.Shared.Logging.LogLevel.Fatal;\n\t\t\tcase LogLevel.None:\n\t\t\tdefault:\n\t\t\t\treturn DNA.Shared.Logging.LogLevel.Info;\n\t\t}\n\t}\n}\n\npublic sealed class CustomLoggerProvider : ILoggerProvider\n{\n\tprivate static readonly CustomLogger logger = new();\n\tpublic ILogger CreateLogger(string categoryName) => logger;\n\tpublic void Dispose() { }\n}<\/pre><\/div>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-5516","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/5516","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/comments?post=5516"}],"version-history":[{"count":1,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/5516\/revisions"}],"predecessor-version":[{"id":5517,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/5516\/revisions\/5517"}],"wp:attachment":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/media?parent=5516"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/categories?post=5516"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/tags?post=5516"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}