private static void ConfigureLog4Net() { if (ConfigurationManager.GetSection("log4net") == null) { var layout = new SimpleLayout(); layout.ActivateOptions(); var consoleAppender = new ConsoleAppender { Layout = layout }; consoleAppender.AddFilter(new LevelRangeFilter { LevelMin = Level.Info, LevelMax = Level.Fatal, AcceptOnMatch = false }); consoleAppender.AddFilter( new LoggerMatchFilter { LoggerToMatch = "Remora", AcceptOnMatch = true } ); consoleAppender.AddFilter(new DenyAllFilter()); consoleAppender.ActivateOptions(); BasicConfigurator.Configure(consoleAppender); } else { XmlConfigurator.Configure(); } }
protected override void Append(LoggingEvent loggingEvent) { if (null == Layout) Layout = new SimpleLayout(); var sb = new StringBuilder(); using (var sr = new StringWriter(sb)) { Layout.Format(sr, loggingEvent); sr.Flush(); try { const string containerName = "transactionlogs"; /* lower casing only */ var blobStorage = BlobStorage.Current; var container = blobStorage.BlobClient.GetContainerReference(containerName); container.CreateIfNotExist(); // Make a unique blob name var filename = String.Format("TRANS-{0}-{1}", GetEventLogName(loggingEvent), DateTime.Now.ToString("ddMMyyyy")).ToUpper(); var blob = container.GetBlobReference(filename); string oldContent; if (!blob.Exists()) { oldContent = String.Empty; } else { using (var reader = new StreamReader(blob.OpenRead())) { oldContent = reader.ReadToEnd(); } } using (var writer = new StreamWriter(blob.OpenWrite())) { writer.Write(oldContent); writer.Write(sb.ToString()); } } catch { System.Diagnostics.Trace.Write(sb.ToString(), GetEventLogName(loggingEvent)); } } }
public static void Configure(string logFilesFolderPath, Log4NetConfigurator configurator) { if (logFilesFolderPath.StartsWith("~")) logFilesFolderPath = logFilesFolderPath.Substring(1); if (logFilesFolderPath.StartsWith("/")) logFilesFolderPath = logFilesFolderPath.Substring(1); var hierarchy = (Hierarchy)LogManager.GetRepository(); hierarchy.Root.Level = Level.Warn; hierarchy.Configured = true; var patternLayout = new PatternLayout { ConversionPattern = "%date [%thread] %-5level %logger - %P{Tenant} - %message%newline" }; patternLayout.ActivateOptions(); var debugFileAppender = BaseFileAppender(patternLayout); debugFileAppender.Name = "debug-file"; debugFileAppender.File = Path.Combine(logFilesFolderPath, "orchard-debug"); debugFileAppender.ActivateOptions(); hierarchy.Root.AddAppender(debugFileAppender); var errorFileAppender = BaseFileAppender(patternLayout); errorFileAppender.Name = "error-file"; errorFileAppender.File = Path.Combine(logFilesFolderPath, "orchard-error"); var levelFilter = new LevelRangeFilter { LevelMin = Level.Error }; levelFilter.ActivateOptions(); errorFileAppender.AddFilter(levelFilter); errorFileAppender.ActivateOptions(); hierarchy.Root.AddAppender(errorFileAppender); var simpleLayout = new SimpleLayout(); simpleLayout.ActivateOptions(); var debuggerAppender = new DebugAppender(); debuggerAppender.Name = "debugger"; debuggerAppender.ImmediateFlush = true; debuggerAppender.Layout = simpleLayout; debuggerAppender.ActivateOptions(); hierarchy.Root.AddAppender(debuggerAppender); GetLogger("Orchard").AddAppender(debuggerAppender); GetLogger("Orchard.Localization").Level = Level.Warn; GetLogger("NHibernate.Cache").Level = Level.Error; GetLogger("NHibernate.AdoNet.AbstractBatcher").Level = Level.Off; GetLogger("NHibernate.Util.ADOExceptionReporter").Level = Level.Off; configurator(hierarchy); }
/// <summary> /// Just a sample program to show the usage of various code analisys tools, documentation tools, and code quality tools. /// </summary> /// <param name="args"></param> public static void Main(string[] args) { var layout = new SimpleLayout(); var appender = new FileAppender(); appender.Layout = layout; appender.File = "log.txt"; appender.AppendToFile = true; appender.ActivateOptions(); BasicConfigurator.Configure(appender); log.Debug("Debug msg"); log.Error("Error msg"); }
static void Main() { #if DEBUG log4net.Config.BasicConfigurator.Configure(); #else ILayout layout = new SimpleLayout(); FileAppender appender = new FileAppender(); appender.Layout = layout; appender.File = "SPDOpWriter.log"; appender.AppendToFile = true; log4net.Config.BasicConfigurator.Configure(appender); #endif log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType).Info("SPD start up: " + DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToShortTimeString()); try { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException); Application.Run(new OPWriterForm()); } catch (Exception e) { handleSPDError(e); } }
static public void SetupTextBoxAppend(TextBox textbox, string sLayerFormat) { TextBoxAppender textBoxAppender = new TextBoxAppender(); textBoxAppender.AppenderTextBox = textbox; textBoxAppender.Threshold = log4net.Core.Level.All; ILayout layout = null; if (string.IsNullOrEmpty(sLayerFormat)) { layout = new log4net.Layout.SimpleLayout(); } else { PatternLayout layoutPattern = new PatternLayout(sLayerFormat); layout = layoutPattern; } textBoxAppender.Layout = layout; textBoxAppender.Name = string.Format("TextBoxAppender_{0}", textbox.Name); textBoxAppender.ActivateOptions(); Hierarchy h = (Hierarchy)log4net.LogManager.GetRepository(); h.Root.AddAppender(textBoxAppender); }
static LogManager() { var layout = new SimpleLayout(); layout.ActivateOptions(); var logAppender = new EventLogAppender {Layout = layout, LogName = EventLogConstants.EVENT_LOG_NAME}; logAppender.ActivateOptions(); var lossyAppender = new BufferingForwardingAppender { Lossy = true, BufferSize = 10, Evaluator = new log4net.Core.LevelEvaluator(log4net.Core.Level.Error) }; lossyAppender.AddAppender(logAppender); lossyAppender.ActivateOptions(); var hierarchy = (Hierarchy) log4net.LogManager.GetRepository(); Logger root = hierarchy.Root; root.Level = log4net.Core.Level.All; BasicConfigurator.Configure(lossyAppender); }
static void Main(string[] args) { var files = new List<string>(); int monitor = 0; int? lines = null; var watch = false; LayoutSkeleton layout = null; var help = false; var p = new OptionSet() { { "i|file=", v => { files.Add(v); } }, { "m|monitor=", v => { monitor=ParseInterval(v);}}, { "w|watch", v => { watch = true;}}, { "l|lines=", v => { lines=Int32.Parse(v);}}, { "h|?|help", v => { help = true;}}, { "y|layout=",v=> { layout=new PatternLayout(v);}}, { "f|format=", v=> { layout = GetFormatLayout(v); }} }; var detectedFiles = args .Where(a => !(a.StartsWith("-") || a.StartsWith("/"))) .Where(a => File.Exists(a)); files.AddRange(detectedFiles); p.Parse(args); if (layout == null) { layout = new SimpleLayout(); } Action<TextWriter,LogEntry> showentry = (writer, l) => layout.Format(writer, new LoggingEvent(l.Data)); if (help) { Console.WriteLine(@"Usage: -i|file={a filename} The (input) file to watch, monitor or -l|lines={tail x lines} Display the last x lines. Defaults to 10 lines. -y|layout={pattern layout syntax as defined in log4net.Layout.PatternLayout} -f|format={a named layout format} The available formats are: -f=minusminus Information delimited by newline and ---------------------- -h|?|help Display help -w|watch Use file system watcher to watch file -m|monitor=seconds Use polling to check for changes of the file. For instance to : LogTail.exe -f=logfile.xml LogTail.exe -file=logfile.xml If you are in powershell (or cygwin) you can do the following: cat yourlogfile.xml | LogTail.exe "); return; } if (watch) { var w = new Watcher<LogEntry>(new FileWithPosition(files.Single()), new LogEntryParser()); w.LogEntry += (entry) => showentry(Console.Out, entry); Do(w); return; } if (monitor > 0) { var w = new Poller<LogEntry>(new FileWithPosition(files.Single()), monitor, new LogEntryParser()); w.LogEntry += (entry) => showentry(Console.Out, entry); Do(w); return; } if (files.Any()) { TailFiles(lines ?? 10, files, (entry) => showentry(Console.Out, entry)); return; } else { Console.WriteLine("No files, listening to standard input."); using (Stream stdin = Console.OpenStandardInput()) using (Stream stdout = Console.OpenStandardOutput()) using (StreamWriter writer = new StreamWriter(stdout)) { var items = new LogEntryParser().Parse(stdin).ToArray(); foreach (var logEntry in items.Skip(items.Count() - (lines ?? 10))) { showentry(writer, logEntry); } } return; } }