Logging trouble [modified]
-
Analyzing traces generated by a web services, I found that multiple null reference exceptions were generated. According to stack traces, the error source was inside the following log method, based on Enterprise Library.
private static void WriteEntry(string category, string message, TraceEventType severity, bool getTitleUsingStackTrace) { try { if (string.IsNullOrEmpty(category) || string.IsNullOrEmpty(message)) { return; } StackTrace stackTrace; StackFrame stackFrame; MethodBase methodBase = null; try { stackTrace = new StackTrace(); stackFrame = stackTrace.GetFrame(2); methodBase = stackFrame.GetMethod(); } catch (Exception) { } LogEntry logEntry = new LogEntry(); if (methodBase != null) { logEntry.Title = string.Format(Resources.ActivityIdFormat, methodBase.ReflectedType.Name, methodBase.Name); } else { logEntry.Title = Resources.Unknown; } logEntry.Categories.Add(category); logEntry.EventId = 300; logEntry.Message = ReplaceCharacters(message); logEntry.Severity = severity; logEntry.Priority = 5; logEntry.ProcessName = Path.GetFileName(logEntry.ProcessName); Logger.Write(logEntry); } catch (Exception ex) { ExceptionHelper.HandleException(ex); } }
Due to the try-catch block containing all the method, the exception did not blocked the service execution, but caused the log to show the exception trace instead to the intended trace message. Strangely enough, the exception was thrown only when the trace method was called inside the web service method, not when the was called inside methods of another classes eventually used by the service. Can you spot the subtle bug before I send the solution? SOLUTION: Using pdbs, we found that the exception was generated in the sring.Format row, so we deduced that the cause was the methodBase.ReflectedType.Name statement: for some reasons (r
-
Analyzing traces generated by a web services, I found that multiple null reference exceptions were generated. According to stack traces, the error source was inside the following log method, based on Enterprise Library.
private static void WriteEntry(string category, string message, TraceEventType severity, bool getTitleUsingStackTrace) { try { if (string.IsNullOrEmpty(category) || string.IsNullOrEmpty(message)) { return; } StackTrace stackTrace; StackFrame stackFrame; MethodBase methodBase = null; try { stackTrace = new StackTrace(); stackFrame = stackTrace.GetFrame(2); methodBase = stackFrame.GetMethod(); } catch (Exception) { } LogEntry logEntry = new LogEntry(); if (methodBase != null) { logEntry.Title = string.Format(Resources.ActivityIdFormat, methodBase.ReflectedType.Name, methodBase.Name); } else { logEntry.Title = Resources.Unknown; } logEntry.Categories.Add(category); logEntry.EventId = 300; logEntry.Message = ReplaceCharacters(message); logEntry.Severity = severity; logEntry.Priority = 5; logEntry.ProcessName = Path.GetFileName(logEntry.ProcessName); Logger.Write(logEntry); } catch (Exception ex) { ExceptionHelper.HandleException(ex); } }
Due to the try-catch block containing all the method, the exception did not blocked the service execution, but caused the log to show the exception trace instead to the intended trace message. Strangely enough, the exception was thrown only when the trace method was called inside the web service method, not when the was called inside methods of another classes eventually used by the service. Can you spot the subtle bug before I send the solution? SOLUTION: Using pdbs, we found that the exception was generated in the sring.Format row, so we deduced that the cause was the methodBase.ReflectedType.Name statement: for some reasons (r
I guess you have no reflection permission over private members in your web service since you normally run under medium trust. private static void WriteEntry the Stackwalk stackTrace = new StackTrace(); stackFrame = stackTrace.GetFrame(2); over your method will cause exceptions. Yours, Alois Kraus
-
I guess you have no reflection permission over private members in your web service since you normally run under medium trust. private static void WriteEntry the Stackwalk stackTrace = new StackTrace(); stackFrame = stackTrace.GetFrame(2); over your method will cause exceptions. Yours, Alois Kraus
If you see the code, this part stackTrace = new StackTrace(); stackFrame = stackTrace.GetFrame(2); is in a try-catch block that catchs all the exceptions, also, methodBase is checked for null, so the exception is not thrown there. Also, the exception is a null reference generated in the posted method, not in any method called by it, meaning that we tried to call methods on some variable or property that was null.