public void TestGetEntryTypeForExistingApplicationName() { var eventAppender = new EventLogAppender { ApplicationName = "Winlogon" }; eventAppender.ActivateOptions(); Assert.Equal(EventLogEntryType.Information, GetEntryType(eventAppender, Level.All)); Assert.Equal( EventLogEntryType.Information, GetEntryType(eventAppender, Level.Debug)); Assert.Equal( EventLogEntryType.Information, GetEntryType(eventAppender, Level.Info)); Assert.Equal( EventLogEntryType.Warning, GetEntryType(eventAppender, Level.Warn)); Assert.Equal( EventLogEntryType.Error, GetEntryType(eventAppender, Level.Error)); Assert.Equal( EventLogEntryType.Error, GetEntryType(eventAppender, Level.Fatal)); Assert.Equal( EventLogEntryType.Error, GetEntryType(eventAppender, Level.Off)); }
public static void InitLogging() { var layout = new PatternLayout { ConversionPattern = "%date [%thread] %-5level %logger - %message%newline" }; layout.ActivateOptions(); var filter = new LevelRangeFilter { LevelMin = Level.Warn, LevelMax = Level.Fatal }; filter.ActivateOptions(); var eventLogAppender = new EventLogAppender { ApplicationName = "NServiceBus Sample Server", Threshold = Level.All, Layout = layout }; eventLogAppender.AddFilter(filter); eventLogAppender.ActivateOptions(); var consoleAppender = new ConsoleAppender { Threshold = Level.Info, Layout = layout }; BasicConfigurator.Configure(consoleAppender); LogManager.Use <Log4NetFactory>(); }
public void ActivateOptionsDisablesAppenderIfSourceDoesntExist() { EventLogAppender eventAppender = new EventLogAppender(); eventAppender.ActivateOptions(); Assert.AreEqual(Level.Off, eventAppender.Threshold); }
private static IAppender GetEventLogAppender() { var appender = new EventLogAppender { Threshold = Level.Error, Layout = GetPattermLayout() }; var mappers = new[] { new EventLogAppender.Level2EventLogEntryType { EventLogEntryType = EventLogEntryType.Error, Level = Level.Error }, new EventLogAppender.Level2EventLogEntryType { EventLogEntryType = EventLogEntryType.Warning, Level = Level.Warn }, new EventLogAppender.Level2EventLogEntryType { EventLogEntryType = EventLogEntryType.Error, Level = Level.Fatal } }; foreach (var mapper in mappers) { appender.AddMapping(mapper); } appender.ActivateOptions(); return(appender); }
public void TestGetEntryType() { EventLogAppender eventAppender = new EventLogAppender(); eventAppender.ActivateOptions(); Assert.AreEqual( EventLogEntryType.Information, GetEntryType(eventAppender, Level.All)); Assert.AreEqual( EventLogEntryType.Information, GetEntryType(eventAppender, Level.Debug)); Assert.AreEqual( EventLogEntryType.Information, GetEntryType(eventAppender, Level.Info)); Assert.AreEqual( EventLogEntryType.Warning, GetEntryType(eventAppender, Level.Warn)); Assert.AreEqual( EventLogEntryType.Error, GetEntryType(eventAppender, Level.Error)); Assert.AreEqual( EventLogEntryType.Error, GetEntryType(eventAppender, Level.Fatal)); Assert.AreEqual( EventLogEntryType.Error, GetEntryType(eventAppender, Level.Off)); }
private static void SetupLogger(string eventSource) { var hierarchy = (Hierarchy)LogManager.GetRepository(); var patternLayout = new PatternLayout { ConversionPattern = "%date [%thread] %-5level %logger - %message%newline" }; patternLayout.ActivateOptions(); if (Environment.UserInteractive) { var coloredConsoleAppender = new ColoredConsoleAppender { Layout = patternLayout }; coloredConsoleAppender.AddMapping(new ColoredConsoleAppender.LevelColors { Level = Level.Debug, ForeColor = ColoredConsoleAppender.Colors.Green }); coloredConsoleAppender.AddMapping(new ColoredConsoleAppender.LevelColors { Level = Level.Info, ForeColor = ColoredConsoleAppender.Colors.White }); coloredConsoleAppender.AddMapping(new ColoredConsoleAppender.LevelColors { Level = Level.Warn, ForeColor = ColoredConsoleAppender.Colors.Yellow }); coloredConsoleAppender.AddMapping(new ColoredConsoleAppender.LevelColors { Level = Level.Error, ForeColor = ColoredConsoleAppender.Colors.White, BackColor = ColoredConsoleAppender.Colors.Red }); coloredConsoleAppender.ActivateOptions(); hierarchy.Root.AddAppender(coloredConsoleAppender); } else { var eventLogAppender = new EventLogAppender { ApplicationName = eventSource, Layout = patternLayout }; eventLogAppender.ActivateOptions(); hierarchy.Root.AddAppender(eventLogAppender); } hierarchy.Root.Level = Level.All; hierarchy.Configured = true; }
private static EventLogAppender GetEventLogAppender(PatternLayout patternLayout) { EventLogAppender eventLogAppender = new EventLogAppender(); eventLogAppender.LogName = EventLogName; eventLogAppender.ApplicationName = ApplicationName; eventLogAppender.Layout = patternLayout; eventLogAppender.Threshold = Level.Warn; eventLogAppender.ActivateOptions(); return(eventLogAppender); }
public EventLogAppender CreateEventLogAppender(Level threshold) { var tracer = new EventLogAppender(); var patternLayout = CreatePatternLayout(FILE_LOG_PATTERN); tracer.Layout = patternLayout; tracer.Name = "EventLogAppender"; tracer.Threshold = threshold; tracer.ApplicationName = _applicationName; tracer.ActivateOptions(); return(tracer); }
public override IAppender GetAppender() { var appender = new EventLogAppender { Threshold = Level.All, Layout = new PatternLayout(layoutString), ApplicationName = ApplicationName, }; appender.ActivateOptions(); return(appender); }
/// <summary> /// Setup the event log appender and attach it to a logger. /// </summary> /// <param name="logger">The logger you want to attach the event log appender to.</param> /// <param name="config">The configuration for the appender.</param> private static void SetupEventLogAppender(log4netLogger logger, ILogConfig config) { #if NET45 var appender = new EventLogAppender(); appender.Layout = eventLoggerLayout; appender.Name = EventLogAppenderName; appender.LogName = EventLogName; appender.ApplicationName = EventLogSourceName; appender.AddFilter(GetNoAuditFilter()); appender.ActivateOptions(); logger.AddAppender(appender); #endif }
internal static EventLogAppender GetEventLogAppender(string logName, string applicationName) { var layout = new PatternLayout("%date [%thread] %-5level %logger [%property{NDC}] - %message%newline"); layout.ActivateOptions(); var appender = new EventLogAppender { ApplicationName = applicationName, LogName = logName, Layout = layout, }; appender.ActivateOptions(); return(appender); }
public static void LoadEventLogAppender() { EventLogAppender appender = new EventLogAppender(); appender.Name = "EventLogAppender"; PatternLayout patternLayout = new PatternLayout(); patternLayout.ConversionPattern = _ConversionPattern; patternLayout.ActivateOptions(); appender.Layout = patternLayout; appender.ActivateOptions(); BasicConfigurator.Configure(appender); }
/// <summary> /// A temporary event log appender for logging during startup (before config is loaded) /// </summary> /// <param name="logger"></param> private static void SetupTemporaryEventLogAppender(log4netLogger logger) { #if NET45 var appender = new EventLogAppender(); appender.Layout = eventLoggerLayout; appender.Name = TemporaryEventLogAppenderName; appender.LogName = EventLogName; appender.ApplicationName = EventLogSourceName; appender.Threshold = Level.Warn; appender.AddFilter(GetNoAuditFilter()); appender.ActivateOptions(); logger.AddAppender(appender); #endif }
// Configure the event log logger programmatically. public static void ConfigureEventLogAppender(ILog log, string sourceName, Level level) { bool isConfigured = log.Logger.Repository.GetAppenders().OfType <EventLogAppender>().Any(); if (!isConfigured) { // Setup RollingFileAppender EventLogAppender appender = new EventLogAppender(); appender.Layout = new log4net.Layout.PatternLayout(@"[%thread] - %message%newline"); appender.ApplicationName = sourceName; appender.Name = "EventLogAppender"; appender.Threshold = level; appender.ActivateOptions(); BasicConfigurator.Configure(appender); } }
public static ILog CreateAuditTrail(string appName, string logSection = "CustomEventLog", string loggerName = null, [CallerMemberName] string caller = null) { var eventLogAppender = new EventLogAppender() { ApplicationName = appName ?? "[SkyNetCloud module]", LogName = logSection, }; var pl = new PatternLayout() { ConversionPattern = _conversionPattern, }; pl.ActivateOptions(); eventLogAppender.Layout = pl; eventLogAppender.ActivateOptions(); LogManager.ResetConfiguration(); log4net.Config.BasicConfigurator.Configure(eventLogAppender); return(LogManager.GetLogger(loggerName ?? caller)); }
public void Integration() { var rep = LogManager.CreateRepository(Guid.NewGuid().ToString()); var eventAppender = new EventLogAppender { ApplicationName = "xUnit", Layout = new SimpleLayout() }; eventAppender.ActivateOptions(); var stringAppender = new OutputDebugStringAppender { Layout = new SimpleLayout() }; stringAppender.ActivateOptions(); BasicConfigurator.Configure(rep, eventAppender, stringAppender); var logger = LogManager.GetLogger(rep.Name, "LoggerThread"); logger.Error(new Exception("exception test")); }
public static void Configure(ServiceConfiguration configuration, string serviceName) { var hierarchy = (Hierarchy)LogManager.GetRepository(); hierarchy.Root.RemoveAllAppenders(); // see http://logging.apache.org/log4net/release/sdk/log4net.Layout.PatternLayout.html var layout = new PatternLayout { ConversionPattern = "%date{dd-MMM-yyyy HH:mm:ss} %-5level %message%n%exception%n" }; Level level; switch (configuration.LogThreshold) { case LogLevel.Info: level = Level.Info; break; case LogLevel.Debug: level = Level.Debug; break; case LogLevel.Warn: level = Level.Warn; break; case LogLevel.Error: level = Level.Error; break; default: throw new Exception("Unhandled log level"); } //var fileAppender = new RollingFileAppender //{ // Layout = layout, // AppendToFile = true, // RollingStyle = RollingFileAppender.RollingMode.Composite, // DatePattern = "yyyy-MM-dd", // File = Path.Combine(configuration.ImporterLogFolder ?? String.Empty, "NBNAutoImportlog"), // LockingModel = new FileAppender.MinimalLock(), // MaxFileSize = 2097152, //2Mb // CountDirection = 1, // StaticLogFileName = false, // //File = settings.LogDirectory // Threshold = level //}; var eventLogAppender = new EventLogAppender() { ApplicationName = serviceName, Layout = layout, Threshold = level }; var consoleAppender = new ConsoleAppender { Layout = layout, }; layout.ActivateOptions(); eventLogAppender.ActivateOptions(); log4net.Config.BasicConfigurator.Configure(eventLogAppender); //fileAppender.ActivateOptions(); //log4net.Config.BasicConfigurator.Configure(fileAppender); consoleAppender.ActivateOptions(); log4net.Config.BasicConfigurator.Configure(consoleAppender); }