{"id":479,"date":"2003-08-22T21:58:15","date_gmt":"2003-08-23T01:58:15","guid":{"rendered":"http:\/\/wordpress.cephas.net\/?p=479"},"modified":"2003-08-22T21:58:15","modified_gmt":"2003-08-23T01:58:15","slug":"logging-in-c","status":"publish","type":"post","link":"https:\/\/cephas.net\/blog\/2003\/08\/22\/logging-in-c\/","title":{"rendered":"Logging in C#"},"content":{"rendered":"<p>I wrote up a very simple C# class that provides logging for C# applications since the <a href=\"http:\/\/sourceforge.net\/projects\/csharp-logger\/\">C# Logger<\/a> on sourceforge is still in alpha (and doesn&#8217;t appear to be active) and the <a href=\"http:\/\/msdn.microsoft.com\/library\/default.asp?url=\/library\/en-us\/cpref\/html\/cpref_start.asp\">.NET framework<\/a> doesn&#8217;t seem to provide normal text file logging outside of the <a href=\"http:\/\/msdn.microsoft.com\/library\/default.asp?url=\/library\/en-us\/cpref\/html\/frlrfsystemdiagnosticseventlogclasstopic.asp\">EventLog<\/a> (correct me if I&#8217;m wrong):<br \/>\n<code><br \/>\nusing System;<br \/>\nusing System.IO;<br \/>\nnamespace com.ignitionlabs.logging {&nbsp;&nbsp;<br \/>\n&nbsp;&nbsp;public class Logger {<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;private static int _ALL = 100;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;private static int _INFO = 75;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;private static int _ERROR = 50;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;private static int _OFF = 0;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;private static String logDirectory = System.Configuration.ConfigurationSettings.AppSettings[\"logging.directory\"].ToString();<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;public static int ALL {<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;get { return _ALL; }<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;}<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;public static int ERROR {<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;get { return _ERROR; }<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;}<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;public static int INFO {<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;get { return _INFO; }<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;}<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;public static int OFF {<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;get { return _OFF; }<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;}<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;public static void append(String message, int level) {<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int logLevel = _OFF;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String strLogLevel = System.Configuration.ConfigurationSettings.AppSettings[\"logging.level\"].ToString();<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;switch(strLogLevel) {<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case \"ALL\":<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;logLevel = _ALL;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case \"ERROR\":<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;logLevel = _ERROR;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case \"INFO\":<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;logLevel = _INFO;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;default:<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;logLevel = _OFF;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (logLevel &gt;= level) {<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;DateTime dt = DateTime.Now;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String filePath = logDirectory + dt.ToString(\"yyyyMMdd\") + \".log\";<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (!File.Exists(filePath)) {<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;FileStream fs = File.Create(filePath);<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fs.Close();<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;try {<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;StreamWriter sw = File.AppendText(filePath);<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sw.WriteLine(dt.ToString(\"hh:mm:ss\") + \"|\" + message);<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sw.Flush();<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sw.Close();<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} catch (Exception e) {<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Console.WriteLine(e.Message.ToString());<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;}<br \/>\n&nbsp;&nbsp;}<br \/>\n}<br \/>\n<\/code><br \/>\nAfter compiling, you&#8217;ll need to add 2 elements to your &lt;appsettings&gt; in web.config:<\/p>\n<p>&lt;add key=&#8221;logDirectory&#8221; value=&#8221;c:\\myapp\\logs\\&#8221; \/&gt;<br \/>\n&lt;add key=&#8221;logLevel&#8221; value=&#8221;ALL|INFO|ERROR|OFF&#8221; \/&gt;<\/p>\n<p>and then you can use the class like this:<\/p>\n<p>Logger.append(&#8220;your application message&#8230;&#8221;, Logger.ALL);<\/p>\n<p>As always, I&#8217;m interested in any design flaws or critiques.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I wrote up a very simple C# class that provides logging for C# applications since the C# Logger on sourceforge is still in alpha (and doesn&#8217;t appear to be active) and the .NET framework doesn&#8217;t seem to provide normal text file logging outside of the EventLog (correct me if I&#8217;m wrong): using System; using System.IO; &hellip; <a href=\"https:\/\/cephas.net\/blog\/2003\/08\/22\/logging-in-c\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Logging in C#<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[17],"tags":[],"_links":{"self":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/posts\/479"}],"collection":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/comments?post=479"}],"version-history":[{"count":0,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/posts\/479\/revisions"}],"wp:attachment":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/media?parent=479"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/categories?post=479"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/tags?post=479"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}