Don’t do this, it’s wrong

Real production code I'm working on. For some reason, the error of writing the error of logging to the writing of the log errors. (Or something like that)

public static class LogHelper {

private static void WriteEventLog(string pMessage, EventLogEntryType pType) { StackTrace st = new StackTrace(); try { LogEntry log = new LogEntry(); log.Message = string.Format(st.GetFrame(1).GetMethod().Name + ": {0}", pMessage); if (pType == EventLogEntryType.Information) //if (category != Category.General) { log.Priority = Priority.Normal; log.Categories.Add(Category.General); } else { log.Priority = Priority.High; log.Categories.Add(Category.Error); } Logger.Write(log); } catch (Exception ex) { LogEntry log = new LogEntry(); log.Message = string.Format(st.GetFrame(1).GetMethod().Name + " : {0}", ex); log.Categories.Add(Category.Error); log.Priority = Priority.High; Logger.Write(log); if (ex.InnerException != null) { log.Message = string.Format("ErrHandler: {0}", ex); Logger.Write(log); } } } 

by SequesterMe via /r/csharp

Leave a Reply