Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Other Discussions
  3. Clever Code
  4. Logging trouble [modified]

Logging trouble [modified]

Scheduled Pinned Locked Moved Clever Code
helpwcfdata-structuresdebuggingquestion
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Mirko1980
    wrote on last edited by
    #1

    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

    A 1 Reply Last reply
    0
    • M Mirko1980

      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

      A Offline
      A Offline
      Alois Kraus
      wrote on last edited by
      #2

      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

      M 1 Reply Last reply
      0
      • A 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

        M Offline
        M Offline
        Mirko1980
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups