[TestMethod()] public void LoggerProxy() { Log.WriteDelay = 1000; var Log2 = new FileLogger("logtest2.tmp", 1000); LoggerProxy proxy = new LoggerProxy(true, Log); proxy.Masters.Add(Log2); proxy.Info("Proxy"); proxy.Flush(); Assert.IsTrue(LogContent.Contains("Proxy"), "Proxy is forwarding to 1st log"); Assert.IsTrue(File.ReadAllText(Log2.LogFile).Contains("Proxy"), "Proxy is forwarding to 2nd log"); proxy.Clear(); Assert.IsFalse(File.Exists(Log.LogFile), "Proxy is clearing 1st Log"); Assert.IsFalse(File.Exists(Log2.LogFile), "Proxy is clearing 2nd Log"); Log2.Level = System.Diagnostics.SourceLevels.Verbose; proxy.Level = System.Diagnostics.SourceLevels.Information; proxy.Verbo("Verbose filtered"); proxy.Flush(); Assert.IsFalse(File.Exists(Log.LogFile), "Proxy is not forwarding low levels"); Assert.IsFalse(File.Exists(Log2.LogFile), "Proxy is not forwarding low levels 2"); proxy.Level = System.Diagnostics.SourceLevels.Verbose; proxy.Verbo("Verbose written"); proxy.Flush(); Assert.IsFalse(File.Exists(Log.LogFile), "Proxy is not writing to low levels"); Assert.IsTrue(File.Exists(Log2.LogFile), "Proxy is writing to low levels 2"); proxy.Clear(); try { new LoggerProxy(false).Clear(); Assert.IsTrue(false, "Proxy failed to disallow clear"); } catch (InvalidOperationException) { Assert.IsTrue(true, "Proxy may disallow clear"); } Assert.AreEqual(null, Error, "OnError"); }
private bool DoLog(string level, object param) { if (LowerAndIsEmpty(level, out level)) { level = param is Exception ? "e" : "i"; } var lv = TraceEventType.Information; switch (level[0]) { case 'c': lv = TraceEventType.Critical; break; case 'e': lv = TraceEventType.Error; break; case 't': lv = TraceEventType.Transfer; break; case 'v': lv = TraceEventType.Verbose; break; case 'w': lv = TraceEventType.Warning; break; case 'f': if ("flush".Equals(level)) { lock (this) if (Logger == null) { return(true); } Logger.Verbo("Flushing log.{0}{1}", param == null ? "" : " Reason: ", param ?? ""); Logger.Flush(); return(true); } break; } Log().Log(lv, param); return(true); }