public static void SetOutputFormat(LogOutputs outputToConfigure, string newFormat) { newFormat = LogUtils.PrepareFormatString(newFormat); switch (outputToConfigure) { case LogOutputs.AspNetTrace: { Configuration.FORMAT_ASPNET = newFormat; break; } case LogOutputs.Console: { Configuration.FORMAT_CONSOLE = newFormat; break; } case LogOutputs.EventLog: { Configuration.FORMAT_EVENTLOG = newFormat; break; } case LogOutputs.LogFile: { Configuration.FORMAT_LOGFILE = newFormat; break; } case LogOutputs.TraceWrite: { Configuration.FORMAT_TRACE = newFormat; break; } case LogOutputs.All: { Configuration.FORMAT_ASPNET = newFormat; Configuration.FORMAT_CONSOLE = newFormat; Configuration.FORMAT_EVENTLOG = newFormat; Configuration.FORMAT_LOGFILE = newFormat; Configuration.FORMAT_TRACE = newFormat; break; } } MessageQueue.Push(1, LogLevels.Verbose, null, String.Format("Log output for {0} changed to: '{1}'.", outputToConfigure, newFormat), null); }
public static LogLevels SetOutputLevel(LogOutputs outputToConfigure, LogLevels newLevel) { LogLevels old = LogLevels.None; switch (outputToConfigure) { case LogOutputs.AspNetTrace: { old = Configuration.LEVEL_ASPNET; Configuration.LEVEL_ASPNET = newLevel; break; } case LogOutputs.Console: { old = Configuration.LEVEL_CONSOLE; Configuration.LEVEL_CONSOLE = newLevel; break; } case LogOutputs.EventLog: { old = Configuration.LEVEL_EVENTLOG; Configuration.LEVEL_EVENTLOG = newLevel; break; } case LogOutputs.LogFile: { old = Configuration.LEVEL_LOGFILE; Configuration.LEVEL_LOGFILE = newLevel; break; } case LogOutputs.TraceWrite: { old = Configuration.LEVEL_TRACE; Configuration.LEVEL_TRACE = newLevel; break; } case LogOutputs.All: { Configuration.LEVEL_ASPNET = newLevel; Configuration.LEVEL_CONSOLE = newLevel; Configuration.LEVEL_EVENTLOG = newLevel; Configuration.LEVEL_LOGFILE = newLevel; Configuration.LEVEL_TRACE = newLevel; break; } } MessageQueue.Push(1, LogLevels.Verbose, null, String.Format("Log level for {0} changed from {1} to {2}.", outputToConfigure, old, newLevel), null); return(old); }
public void TestOutputLevel() { LogOutputs output = Log.Config.Output; try { string messageText = "Warning will robinson, DANGER!"; Log.Config.SetOutputFormat(LogOutputs.All, "{Message}"); Log.Config.SetOutputLevel(LogOutputs.All, LogLevels.Info); LogLevels newLevel = LogLevels.Warning; Assert.AreEqual(LogLevels.Info, Log.Config.SetOutputLevel(LogOutputs.AspNetTrace, newLevel)); Assert.AreEqual(LogLevels.Info, Log.Config.SetOutputLevel(LogOutputs.TraceWrite, newLevel)); Assert.AreEqual(LogLevels.Info, Log.Config.SetOutputLevel(LogOutputs.Console, newLevel)); Assert.AreEqual(LogLevels.Info, Log.Config.SetOutputLevel(LogOutputs.EventLog, newLevel)); Assert.AreEqual(LogLevels.Info, Log.Config.SetOutputLevel(LogOutputs.LogFile, newLevel)); Assert.AreEqual(LogLevels.None, Log.Config.SetOutputLevel(LogOutputs.None, newLevel)); Log.Config.Output = LogOutputs.All & ~LogOutputs.EventLog; TextWriter sw = new StringWriter(), orig = Console.Out; Console.SetOut(sw); string tempFile = Path.GetTempFileName(); string origLog = Log.Config.LogFile; Log.Config.LogFile = tempFile; try { Log.Warning(messageText); Log.Info("Hello"); Log.Verbose("Hello"); } finally { Console.SetOut(orig); Log.Config.LogFile = origLog; } Assert.AreEqual(messageText, _lastTrace.Substring(GetType().FullName.Length + 2)); Assert.AreEqual(messageText, sw.ToString().Trim()); Assert.AreEqual(messageText, File.ReadAllText(tempFile).Trim()); } finally { Log.Config.Output = output; Log.Config.SetOutputLevel(LogOutputs.All, LogLevels.Verbose); Log.Config.SetOutputFormat(LogOutputs.All, "[{ManagedThreadId}] {Level} - {FullMessage}"); } }
/// <summary> Sets appropriate (i hope) defaults to all configuration options </summary> public static void Configure() { // DEFAULT OUTPUT FORMATS: FORMAT_ASPNET = LogUtils.PrepareFormatString("{Level,8} - {Message}"); FORMAT_TRACE = LogUtils.PrepareFormatString("[{ManagedThreadId:D2}] {Level,8} - {Message}{Location}{Exception}"); FORMAT_CONSOLE = LogUtils.PrepareFormatString("[{ManagedThreadId:D2}] {Level,8} - {Message}{Location}{Exception}"); FORMAT_EVENTLOG = LogUtils.PrepareFormatString("[{ManagedThreadId:D2}] {Level,8} - {Message}{Location}{Exception}"); FORMAT_LOGFILE = LogUtils.PrepareFormatString("{EventTime:o} [{ProcessId:D4},{ManagedThreadId:D2}] {Level,8} - {Message}{Location}{Exception}"); //These filters will apply after the Log.Level filter: LEVEL_ASPNET = LogLevels.Verbose; LEVEL_TRACE = LogLevels.Verbose; LEVEL_CONSOLE = LogLevels.Verbose; LEVEL_LOGFILE = LogLevels.Verbose; LEVEL_EVENTLOG = LogLevels.Warning; // INIT DEFAULT LOG LEVELS LogOutput = LogOutputs.LogFile; LogLevel = LogLevels.Verbose; LogOption = LogOptions.Default; InnerFormatter = System.Globalization.CultureInfo.InvariantCulture; CurrentLogFile = DefaultLogFile; FILE_SIZE_THREASHOLD = 10 * 1024 * 1024; // 10mb max log file FILE_MAX_HISTORY_SIZE = 10; // Don't keep more than 10 EventLogName = "Application"; EventLogSource = ProcessName; try { if (System.Web.HttpContext.Current != null && System.Web.HttpContext.Current.Trace.IsEnabled) { LogOutput |= LogOutputs.AspNetTrace; } } catch { } if (IsDebugging) { LogOutput |= LogOutputs.TraceWrite; } // if debugging or running with asp.net's trace mode, add the file info (we know we aren't in a production environment) if ((LogOutput & (LogOutputs.AspNetTrace | LogOutputs.TraceWrite)) != LogOutputs.None) { LogOption |= LogOptions.LogAddFileInfo; } }
/// <summary> /// Serialization Constructor for ISerializable /// </summary> public EventData(SerializationInfo info, StreamingContext context) { string sver = info.GetString("rec:ver"); if (sver != String.Format("{0}:{1}", VersionInfo.FieldCount, VersionInfo.CheckSum)) { throw new SerializationException(); } _data = new object[VersionInfo.FieldCount]; Type t; Dictionary <string, object> values = new Dictionary <string, object>(StringComparer.OrdinalIgnoreCase); foreach (SerializationEntry entry in info) { values.Add(entry.Name, entry.Value); } for (int i = 0; i < _data.Length; i++) { try { if (i == (int)LogFields.Exception && values.ContainsKey("hasError") && (bool)values["hasError"]) { _data[i] = new SharedException(info, context); } object value; if (null != (t = VersionInfo.FieldTypes[i]) && values.TryGetValue(i.ToString(), out value)) { _data[i] = value; } } catch (Exception e) { System.Diagnostics.Trace.WriteLine(e.ToString()); } } //These are used heavily so they are extracted now. _data[0] = this; _data[(int)LogFields.Output] = this.Output = Configuration.LogOutput; _data[(int)LogFields.Level] = Level = (LogLevels)_data[(int)LogFields.Level]; }
public void TestOutput() { LogOutputs defaultOutputs = Log.Config.Output; Assert.AreEqual(defaultOutputs, Log.Config.Output); Log.Config.Output = LogOutputs.TraceWrite; Log.Write("Test Trace"); Assert.AreEqual(GetType().FullName + ": Test Trace", _lastTrace); Assert.AreEqual(LogOutputs.TraceWrite, Log.Config.Output); Log.Config.Output = LogOutputs.None; _lastTrace = null; Log.Write("Test Trace"); Assert.IsNull(_lastTrace); Log.Config.Output = LogOutputs.TraceWrite | defaultOutputs; Assert.AreEqual(LogOutputs.TraceWrite | defaultOutputs, Log.Config.Output); }
public void TestOutputFormat() { LogOutputs output = Log.Config.Output; try { string format = "NOTHING"; Log.Config.SetOutputFormat(LogOutputs.AspNetTrace, format); Log.Config.SetOutputFormat(LogOutputs.TraceWrite, format); Log.Config.SetOutputFormat(LogOutputs.Console, format); Log.Config.SetOutputFormat(LogOutputs.EventLog, format); Log.Config.SetOutputFormat(LogOutputs.LogFile, format); Log.Config.SetOutputFormat(LogOutputs.None, format); Log.Config.Output = LogOutputs.All & ~LogOutputs.EventLog; TextWriter sw = new StringWriter(), orig = Console.Out; Console.SetOut(sw); string tempFile = Path.GetTempFileName(); string origLog = Log.Config.LogFile; Log.Config.LogFile = tempFile; try { Log.Write("Hello"); } finally { Console.SetOut(orig); Log.Config.LogFile = origLog; } Assert.AreEqual(format, _lastTrace.Substring(GetType().FullName.Length + 2)); Assert.AreEqual(format, sw.ToString().Trim()); Assert.AreEqual(format, File.ReadAllText(tempFile).Trim()); } finally { Log.Config.Output = output; Log.Config.SetOutputFormat(LogOutputs.All, "[{ManagedThreadId}] {Level} - {FullMessage}"); } }
/// <summary> Sets appropriate (i hope) defaults to all configuration options </summary> public static void Configure() { // DEFAULT OUTPUT FORMATS: FORMAT_ASPNET = LogUtils.PrepareFormatString("{Level,8} - {Message}"); FORMAT_TRACE = LogUtils.PrepareFormatString("[{ManagedThreadId:D2}] {Level,8} - {Message}{Location}{Exception}"); FORMAT_CONSOLE = LogUtils.PrepareFormatString("[{ManagedThreadId:D2}] {Level,8} - {Message}{Location}{Exception}"); FORMAT_EVENTLOG = LogUtils.PrepareFormatString("[{ManagedThreadId:D2}] {Level,8} - {Message}{Location}{Exception}"); FORMAT_LOGFILE = LogUtils.PrepareFormatString("{EventTime:o} [{ProcessId:D4},{ManagedThreadId:D2}] {Level,8} - {Message}{Location}{Exception}"); //These filters will apply after the Log.Level filter: LEVEL_ASPNET = LogLevels.Verbose; LEVEL_TRACE = LogLevels.Verbose; LEVEL_CONSOLE = LogLevels.Verbose; LEVEL_LOGFILE = LogLevels.Verbose; LEVEL_EVENTLOG = LogLevels.Warning; // INIT DEFAULT LOG LEVELS LogOutput = LogOutputs.LogFile; LogLevel = LogLevels.Verbose; LogOption = LogOptions.Default; InnerFormatter = System.Globalization.CultureInfo.InvariantCulture; CurrentLogFile = DefaultLogFile; FILE_SIZE_THREASHOLD = 10 * 1024 * 1024; // 10mb max log file FILE_MAX_HISTORY_SIZE = 10; // Don't keep more than 10 EventLogName = "Application"; EventLogSource = ProcessName; try { if( System.Web.HttpContext.Current != null && System.Web.HttpContext.Current.Trace.IsEnabled ) LogOutput |= LogOutputs.AspNetTrace; } catch { } if( IsDebugging ) LogOutput |= LogOutputs.TraceWrite; // if debugging or running with asp.net's trace mode, add the file info (we know we aren't in a production environment) if( (LogOutput & (LogOutputs.AspNetTrace | LogOutputs.TraceWrite)) != LogOutputs.None ) LogOption |= LogOptions.LogAddFileInfo; }
public static void AddLogOutput(ILogOutput logOutput) => LogOutputs.Add(logOutput);
public static void AddLogOutput <T>() => LogOutputs.Add((ILogOutput)Activator.CreateInstance <T>());
/// <summary> /// Serialization Constructor for ISerializable /// </summary> public EventData(SerializationInfo info, StreamingContext context) { string sver = info.GetString("rec:ver"); if (sver != String.Format("{0}:{1}", VersionInfo.FieldCount, VersionInfo.CheckSum)) throw new SerializationException(); _data = new object[VersionInfo.FieldCount]; Type t; Dictionary<string, object> values = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase); foreach (SerializationEntry entry in info) values.Add(entry.Name, entry.Value); for (int i = 0; i < _data.Length; i++) { try { if (i == (int)LogFields.Exception && values.ContainsKey("hasError") && (bool)values["hasError"]) _data[i] = new SharedException(info, context); object value; if (null != (t = VersionInfo.FieldTypes[i]) && values.TryGetValue(i.ToString(), out value)) _data[i] = value; } catch (Exception e) { System.Diagnostics.Trace.WriteLine(e.ToString()); } } //These are used heavily so they are extracted now. _data[0] = this; _data[(int)LogFields.Output] = this.Output = Configuration.LogOutput; _data[(int)LogFields.Level] = Level = (LogLevels)_data[(int)LogFields.Level]; }
public static void SetOutputFormat(LogOutputs outputToConfigure, string newFormat) { newFormat = LogUtils.PrepareFormatString(newFormat); switch(outputToConfigure) { case LogOutputs.AspNetTrace: { Configuration.FORMAT_ASPNET = newFormat; break; } case LogOutputs.Console: { Configuration.FORMAT_CONSOLE = newFormat; break; } case LogOutputs.EventLog: { Configuration.FORMAT_EVENTLOG = newFormat; break; } case LogOutputs.LogFile: { Configuration.FORMAT_LOGFILE = newFormat; break; } case LogOutputs.TraceWrite: { Configuration.FORMAT_TRACE = newFormat; break; } case LogOutputs.All: { Configuration.FORMAT_ASPNET = newFormat; Configuration.FORMAT_CONSOLE = newFormat; Configuration.FORMAT_EVENTLOG = newFormat; Configuration.FORMAT_LOGFILE = newFormat; Configuration.FORMAT_TRACE = newFormat; break; } } MessageQueue.Push(1, LogLevels.Verbose, null, String.Format("Log output for {0} changed to: '{1}'.", outputToConfigure, newFormat), null); }
public static LogLevels SetOutputLevel(LogOutputs outputToConfigure, LogLevels newLevel) { LogLevels old = LogLevels.None; switch(outputToConfigure) { case LogOutputs.AspNetTrace: { old = Configuration.LEVEL_ASPNET; Configuration.LEVEL_ASPNET = newLevel; break; } case LogOutputs.Console: { old = Configuration.LEVEL_CONSOLE; Configuration.LEVEL_CONSOLE = newLevel; break; } case LogOutputs.EventLog: { old = Configuration.LEVEL_EVENTLOG; Configuration.LEVEL_EVENTLOG = newLevel; break; } case LogOutputs.LogFile: { old = Configuration.LEVEL_LOGFILE; Configuration.LEVEL_LOGFILE = newLevel; break; } case LogOutputs.TraceWrite: { old = Configuration.LEVEL_TRACE; Configuration.LEVEL_TRACE = newLevel; break; } case LogOutputs.All: { Configuration.LEVEL_ASPNET = newLevel; Configuration.LEVEL_CONSOLE = newLevel; Configuration.LEVEL_EVENTLOG = newLevel; Configuration.LEVEL_LOGFILE = newLevel; Configuration.LEVEL_TRACE = newLevel; break; } } MessageQueue.Push(1, LogLevels.Verbose, null, String.Format("Log level for {0} changed from {1} to {2}.", outputToConfigure, old, newLevel), null); return old; }