Join 34,000+ subscribers and receive articles from our blog about software quality, testing, QA and security.

Filtering By Namespace/Stack without using ProcessFlow


Our organization already uses the .NET diagnostics framework to perform tracing this is somewhat limited in the scope of tracing that can be accomplished. I have found it difficult to get the executing method into the trace output and to allow it to be filtered. Support suggested that I use the session name as a method to do this. Below is a snippet of code I used incase anyone else may run into a similar issue:

//NOTE: We dont use eventCache.Callstack in any other configured TraceListener 
//If eventCache.Callstack is called it will result in two stack traces can be an expensive
                StackTrace stack = new StackTrace(SkipFrames, true);
                using (DataViewerContext context = new DataViewerContext())
                    if (Callstack)
                    LogEntry entry = new LogEntry();
                    entry.ViewerId = context.ViewerId;
                    entry.ProcessId = eventCache.ProcessId;
                    entry.ThreadId = Int32.Parse(eventCache.ThreadId, NumberStyles.HexNumber);
                    entry.Timestamp = Session.Parent.Now();
                    entry.Color = Session.Color;
                    entry.Level = level;
                    entry.LogEntryType = TranslateEntryType(eventType);
//Replace session name with stack frame method DeclaringType or prefix with CustomAttribute
                    if(stack.FrameCount > 0) entry.SessionName = stack.GetFrame(0).GetMethod().DeclaringType.FullName;
                    if (!String.IsNullOrEmpty(Session.Name)) entry.SessionName = String.Concat('[',Session.Name, ']', entry.SessionName);
                    entry.Title = message;
                    entry.Data = context.ViewerData;


Looks good! Thanks for sharing it.