public TraceEventArgs(TraceEntry entry) { if(entry == null) throw new ArgumentNullException("entry"); _entry = entry; }
public override void OnTrace(TraceEntry entry) { if(entry == null) return; System.Diagnostics.EventLog.WriteEntry(entry.Source, entry.ToString(), GetEventLogEntryType(entry.EntryType)); }
public override void OnTrace(TraceEntry entry) { if(entry == null) return; ConsoleColor foregroundColor = Console.ForegroundColor; switch(entry.EntryType) { case TraceEntryType.Warning: Console.ForegroundColor = ConsoleColor.Yellow; break; case TraceEntryType.Failure: Console.ForegroundColor = ConsoleColor.Magenta; break; case TraceEntryType.Error: Console.ForegroundColor = ConsoleColor.Red; break; } try { Console.WriteLine(entry.ToString(false)); } finally { Console.ForegroundColor = foregroundColor; } }
/// <summary> /// 将日志记录项追加到指定的日志文件中。 /// </summary> /// <param name="filePath">要写入的日志文件,如果文件不存在则新建它。</param> /// <param name="entry">要写入的日志记录项。</param> /// <exception cref="System.ArgumentNullException">filePath 是空(null)或空字符串(""),entry 是空(null)。</exception> public void WriteEntry(string filePath, TraceEntry entry) { if(string.IsNullOrEmpty(filePath)) throw new ArgumentNullException("filePath"); if(entry == null) throw new ArgumentNullException("entry"); filePath = this.GetFilePath(filePath); using(FileStream stream = new FileStream(filePath, FileMode.Append, FileAccess.Write, FileShare.Read, DefaultBufferSize, false)) { using(var writer = new StreamWriter(stream, Encoding.UTF8, DefaultBufferSize)) { writer.WriteLine("[{0}] {1}, {2}@{3}", entry.EntryType.ToString(), entry.Timestamp.ToString(), Environment.UserName, Environment.MachineName); writer.WriteLine("Message: {0}", entry.Message); if(!string.IsNullOrEmpty(entry.StackTrace)) { writer.WriteLine("StackTrace:"); writer.WriteLine(entry.StackTrace); } if(entry.Data != null) { writer.WriteLine(this.GetTypeName(entry.Data.GetType())); writer.WriteLine("{"); writer.Flush(); //创建文本序列化器 var serializer = this.Serializer; //将实体的数据对象序列化到日志文件中 if(serializer != null) serializer.Serialize(stream, entry.Data); writer.WriteLine("}"); } } } }
public override void OnTrace(TraceEntry entry) { if(entry == null) return; if(!Directory.Exists(_logsDirectory)) Directory.CreateDirectory(_logsDirectory); string fileName = (string.IsNullOrWhiteSpace(entry.Source) ? "nosource" : entry.Source) + ".log"; char[] invalidChars = System.IO.Path.GetInvalidFileNameChars(); foreach(char invalidChar in invalidChars) fileName.Replace(invalidChar.ToString(), string.Empty); this.WriteEntry(Path.Combine(_logsDirectory, fileName), entry); }
public abstract void OnTrace(TraceEntry entry);
protected void OnTraced(TraceEntry entry) { try { TraceEventArgs args = new TraceEventArgs(entry); this.OnTraced(args); } catch(Exception ex) { this.OnFailed(new FailureEventArgs(ex, entry)); } }
protected bool OnTracing(TraceEntry entry) { try { TracingEventArgs args = new TracingEventArgs(entry); this.OnTracing(args); return args.Cancel; } catch(Exception ex) { this.OnFailed(new FailureEventArgs(ex, entry)); return false; } }
public void Trace(TraceEntry entry) { if(entry == null) throw new ArgumentNullException("entry"); //设置跟踪实体对象的StackTrace属性 if(string.IsNullOrEmpty(entry.StackTrace)) entry.StackTrace = this.GetStackTrace(); //激发“Tracing”事件 if(this.OnTracing(entry)) return; foreach(var listener in _listeners) { if(listener != null) this.DoListen(listener, entry); } //激发“Traced”事件 this.OnTraced(entry); }
public bool Assert(bool condition, TraceEntry entry) { if(entry == null) throw new ArgumentNullException("entry"); if(!condition) this.Trace(entry); return !condition; }
public TracingEventArgs(TraceEntry entry, bool cancel) : base(entry) { }
public TracingEventArgs(TraceEntry entry) : this(entry, false) { }
private void DoListen(ITraceListener listener, TraceEntry entry) { if(listener == null) return; bool shouldTrace = true; try { if(listener.Filter != null) shouldTrace = listener.Filter.ShouldTrace(entry); } catch(Exception ex) { this.OnFailed(new FailureEventArgs(ex, listener.Filter)); } try { if(shouldTrace) listener.OnTrace(entry); } catch(Exception ex) { this.OnFailed(new FailureEventArgs(ex, listener)); } }