public void Write(LogScope scope, Verbosity verbosity, string source, string text) { if((int)minimal_verbosity > (int)verbosity || writer == null) return; lock(writer) { UpdateScope(scope); int lvl = (scope != null) ? scope.Level : 0; StringBuilder sb = new StringBuilder(); for(int i=0; i<lvl; i++) sb.Append(" | "); string spaces = sb.ToString(); writer.WriteLine("{0} - {1}: {2} at {3}", spaces, verbosity, text, source); } }
public LogScope(LogScope ParentScope, string Source, string Text) { this.CreateDateTime = DateTime.Now; this.ParentScope = ParentScope; this.Source = Source; this.Text = Text; this.Thread = Thread.CurrentThread; if(this.ParentScope != null) { if(this.ParentScope.ChildScope != null) throw new ApplicationException("Log scope error"); this.ParentScope.ChildScope = this; this.Level = this.ParentScope.Level + 1; } else { this.Level = 0; } }
private void UpdateScope(LogScope scope) { if(last_scope == scope) return; if(scope == null) { last_scope = null; return; } last_scope = scope; while(last_scope.ParentScope != null) last_scope = last_scope.ParentScope; while(last_scope != null) { StringBuilder sb = new StringBuilder(); for(int i=0; i<scope.Level; i++) sb.Append(" | "); writer.WriteLine("{0} {1} at {2}", sb.ToString(), scope.Text, scope.Source); last_scope = last_scope.ChildScope; } last_scope = scope; }
private static void ScopeDisposed(object sender, EventArgs e) { LogScope scope = (LogScope)sender; current_scope = scope.ParentScope; }
public static LogScope ScopeSource(string source, string text) { current_scope = new LogScope(current_scope, source, text); current_scope.Disposed += ScopeDisposed; return current_scope; }