private void InitTrace(string traceName, TracingOutput output) { Trace.AutoFlush = true; var folder = new DirectoryInfo("Logs\\"); if (!folder.Exists) { folder.Create(); } if (output.HasFlag(TracingOutput.File)) { var streamWriter = new StreamWriter(Path.Combine(folder.FullName, traceName + ".log"), append: true); var fileListener = new TextWriterTraceListener(streamWriter); fileListener.TraceOutputOptions = TraceOptions.ThreadId | TraceOptions.DateTime; _customTraceListeners.Add(fileListener); System.Diagnostics.Debug.Listeners.Add(fileListener); } if (output.HasFlag(TracingOutput.Console)) { var consoleListener = new ColoredConsoleTraceListener(); _customTraceListeners.Add(consoleListener); System.Diagnostics.Debug.Listeners.Add(consoleListener); } }
public void Initialize(string loggerName, TracingOutput tracingOutput, bool debugMode) { _loggerName = loggerName; var hierarchy = (Hierarchy)LogManager.GetRepository(); if (tracingOutput.HasFlag(TracingOutput.File)) { PatternLayout patternLayout = new PatternLayout(); patternLayout.ConversionPattern = "%date [%thread] %-5level %logger - %message%newline"; patternLayout.ActivateOptions(); RollingFileAppender roller = new RollingFileAppender(); roller.AppendToFile = true; roller.File = "Logs\\" + loggerName + "\\"; roller.Layout = patternLayout; roller.ImmediateFlush = true; roller.DatePattern = "yyyyMM/yyyy-MM-dd'.log'"; roller.RollingStyle = RollingFileAppender.RollingMode.Date; roller.StaticLogFileName = false; roller.ActivateOptions(); hierarchy.Root.AddAppender(roller); } if (tracingOutput.HasFlag(TracingOutput.Console)) { PatternLayout patternLayout = new PatternLayout(); patternLayout.ConversionPattern = "%date %-5level: %message%newline"; patternLayout.ActivateOptions(); var appender = new ColoredConsoleAppender(); appender.AddMapping(CreateColorMapping(Level.Fatal, ColoredConsoleAppender.Colors.Purple | ColoredConsoleAppender.Colors.HighIntensity)); appender.AddMapping(CreateColorMapping(Level.Error, ColoredConsoleAppender.Colors.Red | ColoredConsoleAppender.Colors.HighIntensity)); appender.AddMapping(CreateColorMapping(Level.Warn, ColoredConsoleAppender.Colors.Yellow | ColoredConsoleAppender.Colors.HighIntensity)); appender.AddMapping(CreateColorMapping(Level.Info, ColoredConsoleAppender.Colors.Green | ColoredConsoleAppender.Colors.HighIntensity)); appender.AddMapping(CreateColorMapping(Level.Debug, ColoredConsoleAppender.Colors.Blue | ColoredConsoleAppender.Colors.HighIntensity)); appender.Layout = patternLayout; appender.ActivateOptions(); hierarchy.Root.AddAppender(appender); } if (debugMode) { hierarchy.Root.Level = Level.Debug; } else { hierarchy.Root.Level = Level.Info; } hierarchy.Configured = true; }