{"id":5417,"date":"2017-09-25T20:39:20","date_gmt":"2017-09-25T20:39:20","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=5417"},"modified":"2017-09-25T20:39:20","modified_gmt":"2017-09-25T20:39:20","slug":"serilog-revisited-now-version-2-5","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/serilog-revisited-now-version-2-5\/","title":{"rendered":"Serilog revisited (now version 2.5)"},"content":{"rendered":"<p>About a year ago, I wrote the post <a href=\"http:\/\/putridparrot.com\/blog\/category\/serilog\/\" rel=\"noopener\" target=\"_blank\">Structured logging with Serilog<\/a> which covered the basics of using Serilog 2.0. I&#8217;ve just revisited this post and found Serilog 2.5 has changed things a little. <\/p>\n<p>I&#8217;m not going to go over what Serilog does etc. but instead just list the same code from my original post, but working with the latest NuGet packages.<\/p>\n<p>So install the following packages into your application using NuGet<\/p>\n<ul>\n<li>Serilog<\/li>\n<li>Serilog.Sinks.RollingFile<\/li>\n<li>Serilog.Sinks.Console<\/li>\n<\/ul>\n<p>Serilog.Sinks.Console is used instead of Serilog.Sinks.Literate now, which has been deprecated.<\/p>\n<p>Here&#8217;s the code. The only real change is around the JsonFormatter <\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nLog.Logger = new LoggerConfiguration()\r\n   .WriteTo.Console()\r\n   .WriteTo.RollingFile(\r\n      new JsonFormatter(renderMessage: true), \r\n      &quot;logs\\\\sample-{Date}.txt&quot;)\r\n   .MinimumLevel.Verbose()\r\n   .CreateLogger();\r\n\r\nLog.Logger.Information(&quot;Application Started&quot;);\r\n\r\nfor (var i = 0; i &lt; 10; i++)\r\n{\r\n   Log.Logger.Information(&quot;Iteration {I}&quot;, i);\r\n}\r\n\r\nLog.Logger.Information(&quot;Exiting Application&quot;);\r\n<\/pre>\n<p>Now, let&#8217;s add something new to this post, add the following NuGet package<\/p>\n<ul>\n<li>Serilog.Settings.AppSettings<\/li>\n<\/ul>\n<p>In my original post I pointed out that Serilog seemed to be aimed towards configuration through code, but the Serilog.Settings.AppSettings package allows us to use the App.config for our configuration.<\/p>\n<p>Change you Log.Logger code to the following<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nLog.Logger = new LoggerConfiguration()\r\n   .ReadFrom.AppSettings()\r\n   .CreateLogger();\r\n<\/pre>\n<p>and now in your App.config, within the configuration section, put the following<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;appSettings&gt;\r\n   &lt;add key=&quot;serilog:minimum-level&quot; \r\n        value=&quot;Verbose&quot; \/&gt;\r\n   &lt;add key=&quot;serilog:using:RollingFile&quot; \r\n        value=&quot;Serilog.Sinks.RollingFile&quot; \/&gt;\r\n   &lt;add key=&quot;serilog:write-to:RollingFile.pathFormat&quot;\r\n        value=&quot;logs\\\\sample-{Date}.txt&quot; \/&gt;\r\n   &lt;add key=&quot;serilog:write-to:RollingFile.formatter&quot; \r\n        value=&quot;Serilog.Formatting.Json.JsonFormatter&quot; \/&gt;\r\n   &lt;add key=&quot;serilog:using:Console&quot; \r\n        value=&quot;Serilog.Sinks.Console&quot; \/&gt;\r\n   &lt;add key=&quot;serilog:write-to:Console&quot; \/&gt;\r\n&lt;\/appSettings&gt;\r\n<\/pre>\n<p>This recreates our original code by outputting to both a rolling file and console output, bad sadly does not allow us to set the renderMessage parameter of the JsonFormatter.<\/p>\n<p><em>Note: At the time of writing it appears there&#8217;s no way to set this renderMessage, see <a href=\"https:\/\/stackoverflow.com\/questions\/37794854\/how-to-set-formatprovider-property-in-serilog-from-app-config-file\" rel=\"noopener\" target=\"_blank\">How to set formatProvider property in Serilog from app.config file<\/a>.<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>About a year ago, I wrote the post Structured logging with Serilog which covered the basics of using Serilog 2.0. I&#8217;ve just revisited this post and found Serilog 2.5 has changed things a little. I&#8217;m not going to go over what Serilog does etc. but instead just list the same code from my original post, [&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":[126,125],"tags":[],"class_list":["post-5417","post","type-post","status-publish","format-standard","hentry","category-logging","category-serilog"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/5417","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=5417"}],"version-history":[{"count":9,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/5417\/revisions"}],"predecessor-version":[{"id":5430,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/5417\/revisions\/5430"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=5417"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=5417"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=5417"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}