//Main using static Logger.Logging; Log(); Info("Application started.", "NetworkPing"); Warning("Application about to exit!", "MyApp"); Error(ex, "MyApp"); //Logger.cs using System; using System.Diagnostics; using System.IO; class Logging { static readonly string LogDirectory = Path.GetTempPath() + "\\" + AppDomain.CurrentDomain.FriendlyName; internal static string LogFile = LogDirectory + @"\Application.log"; static readonly string LogBak = LogDirectory + @"\Application.log.bak"; public static void Log() { if (!Directory.Exists(LogDirectory)) { Directory.CreateDirectory(LogDirectory); } if (File.Exists(LogBak)) { File.Delete(LogBak); } if (File.Exists(LogFile)) { File.Copy(LogFile, LogBak); File.Delete(LogFile); } Trace.Listeners.Clear(); TextWriterTraceListener twtl = null; try { twtl = new TextWriterTraceListener(LogFile); ConsoleTraceListener ctl = null; try { ctl = new ConsoleTraceListener(false); Trace.Listeners.Add(twtl); Trace.Listeners.Add(ctl); Trace.AutoFlush = true; } finally { if (ctl != null) { ctl.Dispose(); } } } finally { if (twtl != null) { twtl.Dispose(); } } } public static void Error(string message, string module) { WriteEntry(message, "error", module); } public static void Error(Exception ex, string module) { WriteEntry(ex.Message, "error", module); } public static void Warning(string message, string module) { WriteEntry(message, "warning", module); } public static void Info(string message, string module) { WriteEntry(message, "info", module); } private static void WriteEntry(string message, string type, string module) { Trace.WriteLine( string.Format("{0} ( {1} - {2} ) | {3}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), type, module, message)); } }