public Log4NetTelemetrySink(Level logLevel, RollingFileAppender.RollingMode rollingMode, string repositoryName, string conversionPattern, string filePattern, long maxFileSizeInBytes, int?maxSizeRollBackups) { if (string.IsNullOrWhiteSpace(repositoryName)) { repositoryName = Properties.Resources.RepositoryName; } if (string.IsNullOrWhiteSpace(conversionPattern)) { conversionPattern = Properties.Resources.ConversionPattern; } if (string.IsNullOrWhiteSpace(filePattern)) { filePattern = Properties.Resources.FilePattern; } if (maxFileSizeInBytes <= 0) { maxFileSizeInBytes = Converter.GetInt64(Properties.Resources.MaxFileSizeInBytes); } if (maxSizeRollBackups == null || maxSizeRollBackups <= 0) { maxSizeRollBackups = Converter.GetInt32(Properties.Resources.MaxSizeRollBackups); } var repo = LogManager.GetAllRepositories().Where(r => r.Name == repositoryName).FirstOrDefault() ?? LogManager.CreateRepository(repositoryName); Hierarchy hierarchy = repo as Hierarchy; hierarchy.Root.RemoveAllAppenders(); PatternLayout patternLayout = new PatternLayout(); patternLayout.ConversionPattern = conversionPattern; patternLayout.ActivateOptions(); RollingFileAppender roller = new RollingFileAppender(); roller.AppendToFile = false; roller.File = filePattern; roller.Layout = patternLayout; roller.MaxFileSize = maxFileSizeInBytes; roller.MaxSizeRollBackups = maxSizeRollBackups.Value; roller.RollingStyle = rollingMode; roller.StaticLogFileName = false; roller.ActivateOptions(); hierarchy.Root.AddAppender(roller); //MemoryAppender memory = new MemoryAppender(); //memory.ActivateOptions(); //hierarchy.Root.AddAppender(memory); //hierarchy.Root.IsEnabledFor(logLevel); hierarchy.Root.Level = Level.All; hierarchy.Configured = true; _logger = LogManager.GetLogger(repositoryName, "Log"); }
private static RollingFileAppender GetNewFileApender(string appenderName, string file, int maxSizeRollBackups, bool appendToFile = true, bool staticLogFileName = false, string maximumFileSize = "5MB", RollingFileAppender.RollingMode rollingMode = RollingFileAppender.RollingMode.Composite, string datePattern = "yyyyMMdd\".txt\"", string layoutPattern = "%d [%t] %-5p %c - %m%n") { RollingFileAppender appender = new RollingFileAppender { LockingModel = new FileAppender.MinimalLock(), Name = appenderName, File = file, AppendToFile = appendToFile, MaxSizeRollBackups = maxSizeRollBackups, MaximumFileSize = maximumFileSize, StaticLogFileName = staticLogFileName, RollingStyle = rollingMode, DatePattern = datePattern }; PatternLayout layout = new PatternLayout(layoutPattern); appender.Layout = layout; layout.ActivateOptions(); appender.ActivateOptions(); return(appender); }
private static RollingFileAppender GetNewFileApender(string appenderName, string file, int maxSizeRollBackups, bool appendToFile = true, bool staticLogFileName = false, string maximumFileSize = "5MB", RollingFileAppender.RollingMode rollingMode = RollingFileAppender.RollingMode.Composite, string layoutPattern = "%d [%t] %-5p %c - %m%n") { var appender = new RollingFileAppender { Name = appenderName, Encoding = System.Text.Encoding.UTF8, File = file, AppendToFile = appendToFile, LockingModel = new FileAppender.MinimalLock(), RollingStyle = rollingMode, //DatePattern = datePattern, StaticLogFileName = staticLogFileName, MaxSizeRollBackups = maxSizeRollBackups, MaximumFileSize = maximumFileSize, CountDirection = 0, Threshold = Level.All }; var layout = new PatternLayout(layoutPattern) { Header = "[Header]" + Environment.NewLine, Footer = "[Footer]" + Environment.NewLine }; appender.Layout = layout; var filter = new LevelRangeFilter { LevelMin = Level.All, LevelMax = Level.Fatal }; appender.AddFilter(filter); layout.ActivateOptions(); filter.ActivateOptions(); appender.ActivateOptions(); return(appender); }