{"id":8396,"date":"2020-07-10T21:13:16","date_gmt":"2020-07-10T21:13:16","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=8396"},"modified":"2020-07-10T21:13:16","modified_gmt":"2020-07-10T21:13:16","slug":"logging-in-blazor","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/logging-in-blazor\/","title":{"rendered":"Logging in Blazor"},"content":{"rendered":"<p>It&#8217;s not unusual for us to need to output log message to file, console etc. Obviously there&#8217;s going to be a different in capabilities for logging from WASM (essentially on the client browser) and on a server.<\/p>\n<p><strong>Blazor supplies the logging framework<\/strong><\/p>\n<p>Blazor supplies loggin via the ILogger, ILogger<T>, ILoggerFactory and ILoggerProvider interfaces. In most cases you&#8217;ll use the ILogger and ILogger<T>.<\/p>\n<p>Logging capabilities are supplied via the WebAssemblyHostBuilder on WebAssembly and Host on the server (see Program.cs in both project types).<\/p>\n<p>We can therefore use these interfaces\/implementation via injection like this<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n@inject ILogger Logger;\r\n\/\/ OR\r\n@inject ILogger&lt;MyPage&gt; Logger;\r\n<\/pre>\n<p>Alternatively we might use the ILogFactory like this<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n@inject ILoggerFactory LoggerFactory\r\n\r\n\/\/ with the following\r\nvar logger = LoggerFactory.CreateLogger&lt;MyPage&gt;();\r\n<\/pre>\n<p><strong>Logging in WebAssembly<\/strong><\/p>\n<p>With WebAssembly on the client, we&#8217;d obviously expect to be logging to the Browser devtools. Whilst we can actually just use<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nConsole.WriteLine(&quot;Message 1&quot;);\r\nDebug.WriteLine(&quot;Message 2&quot;);\r\n<\/pre>\n<p>the Logging framework offers some extra functionality, such as categorisation of logs etc. To use the ILogger or ILogger<MyPage>, we simply use<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nLogger.LogWarning(&quot;Log a warning&quot;);\r\n<\/pre>\n<p>(we have the ability to log debug, warning, info etc.)<\/p>\n<p><strong>Logging to Server<\/strong><\/p>\n<p>For Blazor Server, the output from the logging will be visible in the console window if running as a standalone application or the output window of Visual Studio. <\/p>\n<p>However if you want your Blazor server application to output to the browser, then you need to use the IJSRuntime, which we can inject like this<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n@inject IJSRuntime JsRuntime\r\n<\/pre>\n<p>and then use the runtime like this<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nJsRuntime.InvokeAsync&lt;string&gt;(&quot;console.log&quot;, &quot;Browser Message&quot;);\r\n<\/pre>\n<p>as you can see, the runtime allows us access to the JavaScript runtime hosting our pages and we can call the <em>console.log<\/em> JavaScript method, passing the parameters for the method call. In our case it&#8217;s just a string &#8220;Browser Message&#8221;.<\/p>\n<p><strong>Adding custom logger<\/strong><\/p>\n<p>To add a custom logger we can use something like this, where the DebugProvider is our implementation of an ILoggingProvider<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nbuilder.Logging.AddProvider(new DebugProvider());\r\n<\/pre>\n<p><strong>Configuration<\/strong><\/p>\n<p>Configuration is placed in the appsettings.json file (remember if you&#8217;re using appsettings.json on WebAssembly client then this should be placed in the wwwroot folder otherwise it&#8217;s not set to the browser). For server the appsettings.json can be in the project folder.<\/p>\n<p>Here&#8217;s the template generated appsettings section for logging<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n&quot;Logging&quot;: {\r\n  &quot;LogLevel&quot;: {\r\n    &quot;Default&quot;: &quot;Trace&quot;,\r\n    &quot;Microsoft&quot;: &quot;Warning&quot;,\r\n    &quot;Microsoft.Hosting.Lifetime&quot;: &quot;Information&quot;\r\n  }\r\n},\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>It&#8217;s not unusual for us to need to output log message to file, console etc. Obviously there&#8217;s going to be a different in capabilities for logging from WASM (essentially on the client browser) and on a server. Blazor supplies the logging framework Blazor supplies loggin via the ILogger, ILogger, ILoggerFactory and ILoggerProvider interfaces. In most [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[305],"tags":[],"class_list":["post-8396","post","type-post","status-publish","format-standard","hentry","category-blazor"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/8396","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=8396"}],"version-history":[{"count":4,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/8396\/revisions"}],"predecessor-version":[{"id":8400,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/8396\/revisions\/8400"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=8396"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=8396"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=8396"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}