{"id":7025,"date":"2022-11-21T16:04:15","date_gmt":"2022-11-21T15:04:15","guid":{"rendered":"https:\/\/solidt.eu\/site\/?p=7025"},"modified":"2022-11-21T16:04:16","modified_gmt":"2022-11-21T15:04:16","slug":"c-minimal-filelogger","status":"publish","type":"post","link":"https:\/\/solidt.eu\/site\/c-minimal-filelogger\/","title":{"rendered":"C# Minimal FileLogger"},"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\">public static class FileLogger\r\n{\r\n    private const string LogFile = @\"C:\\Temp\\Logging\\MyApp.log\";\r\n    private static SemaphoreSlim semaphore = new SemaphoreSlim(1, 1);\r\n\r\n    private static bool LogEnabled = GetLogEnabled();\r\n\r\n    private static bool GetLogEnabled() \r\n    {\r\n        var directory = Path.GetDirectoryName(LogFile);\r\n        return Directory.Exists(directory);\r\n    }\r\n\r\n    private static void Log(string level, string message, Exception ex) {\r\n        if (!LogEnabled) return;\r\n        semaphore.Wait();\r\n        try\r\n        {\r\n            var exText = ex != null ? $\"{ex}\\r\\n\" : \"\";\r\n            File.AppendAllText(LogFile, $\"{DateTime.Now:u} [{level}] {message}\\r\\n{exText}\");;\r\n        }\r\n        finally { \r\n            semaphore.Release();\r\n        }\r\n    }\r\n\r\n    public static void Debug(string message, Exception ex = null) => Log(\"DEBUG\", message, ex);\r\n    public static void Info(string message, Exception ex = null) => Log(\"INFO\", message, ex);\r\n    public static void Warning(string message, Exception ex = null) => Log(\"WARNING\", message, ex);\r\n    public static void Error(string message, Exception ex = null) => Log(\"ERROR\", message, ex);\r\n}<\/pre><\/div>\n\n\n\n<p><\/p>\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-7025","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/7025","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=7025"}],"version-history":[{"count":1,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/7025\/revisions"}],"predecessor-version":[{"id":7026,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/7025\/revisions\/7026"}],"wp:attachment":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/media?parent=7025"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/categories?post=7025"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/tags?post=7025"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}