public void Error(string message, string category = null, string source = null)
 {
     if (traceSource.Switch.ShouldTrace(TraceEventType.Error))
     {
         var entry = new TraceEntry()
         {
             TraceEntryType = TraceEntryType.Message, Message = message, Source = source ?? this.Source, Category = category, SourceLevel = SourceLevels.Error, CodeSection = this
         };
         foreach (TraceListener listener in traceSource.Listeners)
         {
             listener.WriteLine(entry);
             listener.Flush();
         }
     }
 }
 public void Exception(Exception exception, string category = null, string source = null)
 {
     if (traceSource.Switch.ShouldTrace(TraceEventType.Critical))
     {
         var entry = new TraceEntry()
         {
             TraceEntryType = TraceEntryType.Message, Message = $"Exception: {exception?.ToString()}\nInnerException: {exception?.InnerException?.Message}\nStackTrace: {exception?.StackTrace}", Source = source ?? this.Source, Category = category, SourceLevel = SourceLevels.Critical, CodeSection = this
         };
         foreach (TraceListener listener in traceSource.Listeners)
         {
             listener.WriteLine(entry);
             listener.Flush();
         }
     }
 }
        public CodeSection(Type type,
                           string name                             = null,
                           object payload                          = null,
                           TraceSource traceSource                 = null,
                           TraceEventType traceEventType           = TraceEventType.Verbose,
                           SourceLevels sourceLevel                = SourceLevels.Verbose,
                           string category                         = null,
                           string source                           = null,
                           [CallerMemberName] string memberName    = "",
                           [CallerFilePath] string sourceFilePath  = "",
                           [CallerLineNumber] int sourceLineNumber = 0)
        {
            this.Name             = name;
            this.Payload          = payload;
            this.traceSource      = traceSource;
            this.traceEventType   = traceEventType;
            this.SourceLevel      = sourceLevel;
            this.MemberName       = memberName;
            this.SourceFilePath   = sourceFilePath;
            this.sourceLineNumber = sourceLineNumber;
            this.T        = type;
            this.Category = category;

            if (string.IsNullOrEmpty(source))
            {
                source = type.Assembly.GetName().Name;
            }
            this.Source = source;
            this.CallStartMilliseconds = stopwatch.ElapsedMilliseconds;
            string section = !string.IsNullOrEmpty(this.Name) ? this.Name : null;

            if (traceSource.Switch.ShouldTrace(traceEventType))
            {
                var message = string.Format("{0}.{1}{2}({3}) START", !string.IsNullOrWhiteSpace(T.Name) ? T.Name : string.Empty, this.MemberName, section, this.Payload);
                var entry   = new TraceEntry()
                {
                    TraceEntryType = TraceEntryType.StartCall, Message = message, Source = source, Category = category, SourceLevel = sourceLevel, CodeSection = this
                };
                foreach (TraceListener listener in traceSource.Listeners)
                {
                    listener.WriteLine(entry);
                    listener.Flush();
                }
            }
        }
        public void Dispose()
        {
            string section = !string.IsNullOrEmpty(this.Name) ? string.Format(".{0}", this.Name) : null;

            if (traceSource.Switch.ShouldTrace(traceEventType))
            {
                var message = string.Format("{0}.{1}{2}() END", !string.IsNullOrWhiteSpace(T.Name) ? T.Name : string.Empty, this.MemberName, section);
                var entry   = new TraceEntry()
                {
                    TraceEntryType = TraceEntryType.EndCall, Message = message, Source = this.Source, Category = this.Category, SourceLevel = this.SourceLevel, CodeSection = this
                };
                foreach (TraceListener listener in traceSource.Listeners)
                {
                    listener.WriteLine(entry);
                    listener.Flush();
                }
            }
        }