/// <summary> /// Configure Log4Net /// </summary> public void Log4NetConfig(bool isConsoleEnabled, string traceFilePath) { if (isConsoleEnabled) { // Add console output log4net.Appender.ConsoleAppender ca = new log4net.Appender.ConsoleAppender(); ca.Layout = new log4net.Layout.PatternLayout("%-5level %message%newline"); ca.Threshold = log4net.Core.Level.All; log4net.Config.BasicConfigurator.Configure(ca); ca.ActivateOptions(); } if (traceFilePath != "") { // Add file output log4net.Appender.RollingFileAppender rfa = new log4net.Appender.RollingFileAppender(); rfa.AppendToFile = true; rfa.File = traceFilePath; rfa.MaxFileSize = 4096000; rfa.MaxSizeRollBackups = 2; rfa.Threshold = log4net.Core.Level.All; rfa.Layout = new log4net.Layout.PatternLayout("%-5level %date{HH:mm:ss,fff} " + "[%thread] %logger (%file:%line) " + "%newline%message%newline%newline"); log4net.Config.BasicConfigurator.Configure(rfa); rfa.ActivateOptions(); } // Set Root level log4net.Repository.Hierarchy.Hierarchy h = (log4net.Repository.Hierarchy.Hierarchy)log4net.LogManager.GetRepository (); log4net.Repository.Hierarchy.Logger rootLogger = h.Root; rootLogger.Level = log4net.Core.Level.All; }
private static void EnableConsoleDebugLog() { log4net.Appender.IAppender appender; var consoleAppender = new log4net.Appender.ColoredConsoleAppender { Layout = new SimpleLayout(), Threshold = Level.All }; try { consoleAppender.ActivateOptions(); appender = consoleAppender; } catch (EntryPointNotFoundException) { var fallbackAppender = new log4net.Appender.ConsoleAppender { Layout = new SimpleLayout(), Threshold = Level.All }; fallbackAppender.ActivateOptions(); appender = fallbackAppender; } var repo = (Hierarchy)log4net.LogManager.GetRepository(); var root = repo.Root; root.AddAppender(appender); repo.Configured = true; log.Info("Verbose logging to console enabled."); }
private static void EnableConsoleDebugLog() { log4net.Appender.IAppender appender; var consoleAppender = new log4net.Appender.ColoredConsoleAppender { Layout = new SimpleLayout(), Threshold = Level.All }; try { consoleAppender.ActivateOptions(); appender = consoleAppender; } catch (EntryPointNotFoundException) { var fallbackAppender = new log4net.Appender.ConsoleAppender { Layout = new SimpleLayout(), Threshold = Level.All }; fallbackAppender.ActivateOptions(); appender = fallbackAppender; } var repo = (Hierarchy)log4net.LogManager.GetRepository(); var root = repo.Root; root.AddAppender(appender); repo.Configured = true; log.Info("Verbose logging to console enabled."); }
static Log() { NativeMethods.AllocConsole(); log4net.Layout.PatternLayout pl = new log4net.Layout.PatternLayout("[MySite] %level %date{HH:mm:ss,fff} - %message%n"); log4net.Appender.ConsoleAppender ca = new log4net.Appender.ConsoleAppender(); ca.Layout = pl; ca.Target = "Console.Out"; ca.ActivateOptions(); log4net.Config.BasicConfigurator.Configure(ca); log = LogManager.GetLogger("Debug"); }
static void SetupConsoleLogging() { var rootLogger = ((log4net.Repository.Hierarchy.Hierarchy)log4net.LogManager.GetRepository()).Root; var consoleAppender = new log4net.Appender.ConsoleAppender { Threshold = log4net.Core.Level.Debug, Layout = new log4net.Layout.PatternLayout("%date [%thread] %-5level %logger [%property{NDC}] - %message%newline") }; consoleAppender.ActivateOptions(); rootLogger.AddAppender(consoleAppender); }
private static log4net.Appender.ConsoleAppender GetConsoleAppender( log4net.Core.Level consoleLogLevel) { log4net.Appender.ConsoleAppender appender = new log4net.Appender.ConsoleAppender(); appender.Name = "Console"; appender.Layout = new log4net.Layout.PatternLayout( "%d{yyyy-MM-dd HH:mm:ss} [%t] %-5p %c - %m%n"); appender.Threshold = consoleLogLevel; appender.ActivateOptions(); return(appender); }
/// <summary> /// 初始化日志 /// </summary> /// <param name="file">日志文件</param> /// <param name="datePattern">日期格式</param> /// <param name="fileLevelStr">文件日志等级,有debug、info、warn、error和fatal</param> /// <param name="consoleLevelStr">Console日志等级</param> /// <returns>返回是否成功</returns> public bool Initialize(string file, string datePattern, string fileLevelStr, string consoleLevelStr) { log4net.Core.Level fileLevel = ConvertLog4NetLevel(fileLevelStr); log4net.Core.Level consoleLevel = ConvertLog4NetLevel(consoleLevelStr); const string DefaultPattern = "%d [%t]%-5p %c - %m%n"; try { log4net.Layout.PatternLayout layout = new log4net.Layout.PatternLayout(DefaultPattern); if (file != string.Empty) { log4net.Appender.RollingFileAppender rfileAppender = new log4net.Appender.RollingFileAppender(); rfileAppender.Layout = layout; rfileAppender.File = file; if (datePattern == string.Empty) { rfileAppender.DatePattern = "yyyyMMdd-HH"; } else { rfileAppender.DatePattern = datePattern; } rfileAppender.RollingStyle = log4net.Appender.RollingFileAppender.RollingMode.Date; //.Composite; rfileAppender.Threshold = fileLevel; rfileAppender.ActivateOptions(); log4net.Config.BasicConfigurator.Configure(rfileAppender); } log4net.Appender.ConsoleAppender consoleAppender = new log4net.Appender.ConsoleAppender(layout); consoleAppender.Threshold = consoleLevel; consoleAppender.ActivateOptions(); log4net.Config.BasicConfigurator.Configure(consoleAppender); return(true); } catch (Exception err) { Console.WriteLine("Error:{0} StackTrace:{1}", err.Message, err.StackTrace); try { HTEventLog.InitFileLog(@"ServerEvents.log"); HTEventLog.SaveLog(HTEventLog.LogTypeEnum.错误, string.Format("Error : {0}\r\nStackTrace : \r\n{1}", err.Message, err.StackTrace), System.Windows.Forms.Application.ProductName); } catch { } return(false); } }
public static void AddLogToConsole(bool basicConfig) { AllocConsole(); var appender = new log4net.Appender.ConsoleAppender(); var layout = new log4net.Layout.PatternLayout { ConversionPattern = DefaultConversionPatern, Header = "Time;Level;Description;" }; layout.ActivateOptions(); appender.Layout = layout; appender.ActivateOptions(); if (basicConfig) log4net.Config.BasicConfigurator.Configure(appender); else ((log4net.Repository.Hierarchy.Hierarchy)LogManager.GetRepository()).Root.AddAppender(appender); }
/// <summary> /// Configure log4net object /// </summary> private static void ConfigLog4Net() { // set log filename string logfilename = Environment.GetFolderPath(System.Environment.SpecialFolder.Personal); logfilename += System.IO.Path.DirectorySeparatorChar; logfilename += System.Reflection.Assembly.GetExecutingAssembly().GetName().Name + ".log"; LogFilePath = logfilename; // attach to repository hierarchy log4net.Repository.Hierarchy.Hierarchy repository = LogManager.GetRepository() as log4net.Repository.Hierarchy.Hierarchy; // check for logging into file if (LogToFile) { // add log file appender log4net.Appender.RollingFileAppender fileappender = new log4net.Appender.RollingFileAppender(); fileappender.Layout = new log4net.Layout.PatternLayout("%-5level %date{yyyy-MM-dd HH:mm:ss,fff} [%thread] %message (%file:%line)%newline"); fileappender.File = logfilename; fileappender.MaxSizeRollBackups = 10; fileappender.MaximumFileSize = "10MB"; fileappender.AppendToFile = true; fileappender.ActivateOptions(); repository.Root.AddAppender(fileappender); } // check for logging into console if (LogToConsole) { // add console appender log4net.Appender.ConsoleAppender consoleappender = new log4net.Appender.ConsoleAppender(); consoleappender.Layout = new log4net.Layout.PatternLayout("%-5level %date{HH:mm:ss} %message%newline"); consoleappender.ActivateOptions(); repository.Root.AddAppender(consoleappender); } // set to log all events repository.Root.Level = log4net.Core.Level.All; repository.Configured = true; repository.RaiseConfigurationChanged(EventArgs.Empty); }
public static void AddLogToConsole(bool basicConfig) { AllocConsole(); var appender = new log4net.Appender.ConsoleAppender(); var layout = new log4net.Layout.PatternLayout { ConversionPattern = DefaultConversionPatern, Header = "Time;Level;Description;" }; layout.ActivateOptions(); appender.Layout = layout; appender.ActivateOptions(); if (basicConfig) { log4net.Config.BasicConfigurator.Configure(appender); } else { ((log4net.Repository.Hierarchy.Hierarchy)LogManager.GetRepository()).Root.AddAppender(appender); } }
public override void Configure(string key, ILoggerRepository repository) { foreach (var level in Levels) { var appender = new log4net.Appender.ConsoleAppender(); var layout = new PatternLayout(Pattern); layout.ActivateOptions(); appender.Layout = layout; appender.Name = key + "_" + level.ToString().ToLower(); var filter = new LevelRangeFilter(); filter.LevelMax = level; filter.LevelMin = level; filter.ActivateOptions(); appender.AddFilter(filter); appender.ActivateOptions(); BasicConfigurator.Configure(repository, appender); } }
private static void SetUpConsoleLogger() { string consolePattern = "%message%newline"; log4net.Layout.PatternLayout layout = new log4net.Layout.PatternLayout(consolePattern); layout.Header = ""; layout.Footer = ""; layout.ActivateOptions(); log4net.Appender.ConsoleAppender consoleAppender = new log4net.Appender.ConsoleAppender(); //consoleAppender.ErrorHandler = new LoggingErrorHandler(); consoleAppender.Name = "Console appender"; consoleAppender.Threshold = log4net.Core.Level.Warn; consoleAppender.Layout = layout; consoleAppender.Target = "Console.Out"; consoleAppender.ActivateOptions(); // A slight bit of hackery in order to programatically set more than one appender (which isn't supported by BasicConfigurator) log4net.Repository.Hierarchy.Logger root = ((log4net.Repository.Hierarchy.Hierarchy)log4net.LogManager.GetRepository()).Root; root.AddAppender(consoleAppender); root.Repository.Configured = true; }
// Constructors public Log4netLoggerFactory(string configFilename = null) { // Setting log4net.MDC.Set("tab", "\t"); // ConfigFileList List <FileInfo> configFileList = new List <FileInfo>(); if (string.IsNullOrEmpty(configFilename) == false) { // GetAll configFileList = FileHelper.GetAllFile(configFilename); if (configFileList == null) { throw new InvalidOperationException("configFileList=null"); } } // Configure if (configFileList.Count > 0) { // Repository var repository = LogManager.GetRepository(System.Reflection.Assembly.GetEntryAssembly()); if (repository == null) { throw new InvalidOperationException("repository=null"); } // XmlConfigurator foreach (var configFile in configFileList) { XmlConfigurator.Configure(repository, configFile); } } else { // Repository var repository = LogManager.GetRepository(System.Reflection.Assembly.GetEntryAssembly()); if (repository == null) { throw new InvalidOperationException("repository=null"); } // PatternLayout var patternLayout = new log4net.Layout.PatternLayout() { ConversionPattern = @"%date{yyyy-MM-dd HH:mm:ss fff} %-5level [%thread] %logger.%property{method}() - %message%newline" }; patternLayout.ActivateOptions(); // ConsoleAppender var consoleAppender = new log4net.Appender.ConsoleAppender() { Layout = patternLayout }; consoleAppender.ActivateOptions(); // FileAppender var fileAppender = new log4net.Appender.RollingFileAppender() { Layout = patternLayout, RollingStyle = RollingMode.Date, File = string.Format("log/{0}", System.Reflection.Assembly.GetEntryAssembly().GetName().Name), DatePattern = " yyyy-MM-dd'.log'", StaticLogFileName = false, AppendToFile = true }; fileAppender.ActivateOptions(); // BasicConfigurator BasicConfigurator.Configure(repository, consoleAppender, fileAppender ); } }
private static void SetUpConsoleLogger() { string consolePattern = "%message%newline"; log4net.Layout.PatternLayout layout = new log4net.Layout.PatternLayout( consolePattern ); layout.Header = ""; layout.Footer = ""; layout.ActivateOptions(); log4net.Appender.ConsoleAppender consoleAppender = new log4net.Appender.ConsoleAppender(); //consoleAppender.ErrorHandler = new LoggingErrorHandler(); consoleAppender.Name = "Console appender"; consoleAppender.Threshold = log4net.Core.Level.Warn; consoleAppender.Layout = layout; consoleAppender.Target = "Console.Out"; consoleAppender.ActivateOptions(); // A slight bit of hackery in order to programatically set more than one appender (which isn't supported by BasicConfigurator) log4net.Repository.Hierarchy.Logger root = ( (log4net.Repository.Hierarchy.Hierarchy)log4net.LogManager.GetRepository() ).Root; root.AddAppender( consoleAppender ); root.Repository.Configured = true; }
/// <summary> /// Configure log4net object /// </summary> private static void ConfigLog4Net() { // set log filename string logfilename = Environment.GetFolderPath(System.Environment.SpecialFolder.Personal); logfilename += System.IO.Path.DirectorySeparatorChar; logfilename += System.Reflection.Assembly.GetExecutingAssembly().GetName().Name + ".log"; LogFilePath = logfilename; // attach to repository hierarchy log4net.Repository.Hierarchy.Hierarchy repository = LogManager.GetRepository() as log4net.Repository.Hierarchy.Hierarchy; // check for logging into file if (LogToFile) { // add log file appender log4net.Appender.RollingFileAppender fileappender = new log4net.Appender.RollingFileAppender(); fileappender.Layout = new log4net.Layout.PatternLayout("%-5level %date{yyyy-MM-dd HH:mm:ss,fff} [%thread] %message (%file:%line)%newline"); fileappender.File = logfilename; fileappender.MaxSizeRollBackups = 10; fileappender.MaximumFileSize = "10MB"; fileappender.AppendToFile = true; fileappender.ActivateOptions(); repository.Root.AddAppender(fileappender); } // check for logging into console if (LogToConsole) { // add console appender log4net.Appender.ConsoleAppender consoleappender = new log4net.Appender.ConsoleAppender(); consoleappender.Layout = new log4net.Layout.PatternLayout("%-5level %date{HH:mm:ss} %message%newline"); consoleappender.ActivateOptions(); repository.Root.AddAppender(consoleappender); } // set to log all events repository.Root.Level = log4net.Core.Level.All; repository.Configured = true; repository.RaiseConfigurationChanged(EventArgs.Empty); }
protected override void ProcessRecord() { //todo set default to scriptname.log string test = MyInvocation.ScriptName; log4net.ILog logger = log4net.LogManager.GetLogger("Powershell"); System.IO.FileInfo logFileInfo = new System.IO.FileInfo(_logConfigFile); log4net.Config.XmlConfigurator.ConfigureAndWatch(logFileInfo); WriteObject(logger); log4net.Appender.IAppender[] appenders = log4net.LogManager.GetRepository().GetAppenders(); foreach (log4net.Appender.IAppender logAppender in appenders) { #region RollingFilleAppender if ((logAppender.GetType() == typeof(log4net.Appender.RollingFileAppender)) && logAppender.Name == "OSCCDefaultRollingFileAppender") { log4net.Appender.FileAppender fileAppender = (log4net.Appender.RollingFileAppender)logAppender; if (MyInvocation.BoundParameters.ContainsKey("LogFileName")) { log4net.Util.PatternString dynamicFileName = new log4net.Util.PatternString(LogFileName); log4net.Util.ConverterInfo adminuiLogConverterInfo = new log4net.Util.ConverterInfo(); adminuiLogConverterInfo.Name = "adminuilog"; adminuiLogConverterInfo.Type = typeof(Log4Net_CMTrace.CMAdminUILogFolderPatternConverter); log4net.Util.ConverterInfo ccmLogConverterInfo = new log4net.Util.ConverterInfo(); ccmLogConverterInfo.Name = "ccmlog"; ccmLogConverterInfo.Type = typeof(Log4Net_CMTrace.CMClientLogFolderPatternConverter); log4net.Layout.PatternLayout newLayout = new log4net.Layout.PatternLayout(); dynamicFileName.AddConverter(adminuiLogConverterInfo); dynamicFileName.AddConverter(ccmLogConverterInfo); dynamicFileName.ActivateOptions(); //fileAppender.File = LogFileName; fileAppender.File = dynamicFileName.Format(); } else { if (MyInvocation.ScriptName != "") { fileAppender.File = MyInvocation.ScriptName + ".log"; } } if (MyInvocation.BoundParameters.ContainsKey("FileLogLevel")) { switch (FileLogLevel.ToLower()) { case "off": fileAppender.Threshold = log4net.Core.Level.Off; break; case "info": fileAppender.Threshold = log4net.Core.Level.Info; break; case "warn": fileAppender.Threshold = log4net.Core.Level.Warn; break; case "error": fileAppender.Threshold = log4net.Core.Level.Error; break; case "debug": fileAppender.Threshold = log4net.Core.Level.Debug; break; default: fileAppender.Threshold = log4net.Core.Level.Info; break; } } else { fileAppender.Threshold = log4net.Core.Level.Info; } fileAppender.ActivateOptions(); } #endregion #region OMSFilleAppender if ((logAppender.GetType() == typeof(log4net.Appender.FileAppender)) && logAppender.Name == "OSCCOMSFileAppender") { log4net.Appender.FileAppender fileAppender = (log4net.Appender.FileAppender)logAppender; if (MyInvocation.BoundParameters.ContainsKey("OMSLogFileName")) { log4net.Util.PatternString dynamicFileName = new log4net.Util.PatternString(LogFileName); log4net.Util.ConverterInfo adminuiLogConverterInfo = new log4net.Util.ConverterInfo(); adminuiLogConverterInfo.Name = "adminuilog"; adminuiLogConverterInfo.Type = typeof(Log4Net_CMTrace.CMAdminUILogFolderPatternConverter); log4net.Util.ConverterInfo ccmLogConverterInfo = new log4net.Util.ConverterInfo(); ccmLogConverterInfo.Name = "ccmlog"; ccmLogConverterInfo.Type = typeof(Log4Net_CMTrace.CMClientLogFolderPatternConverter); log4net.Layout.PatternLayout newLayout = new log4net.Layout.PatternLayout(); dynamicFileName.AddConverter(adminuiLogConverterInfo); dynamicFileName.AddConverter(ccmLogConverterInfo); dynamicFileName.ActivateOptions(); //fileAppender.File = LogFileName; fileAppender.File = dynamicFileName.Format(); } else { if (MyInvocation.ScriptName != "") { fileAppender.File = MyInvocation.ScriptName + ".OMS." + DateTime.Today.ToString("yyyyMMdd") + ".log"; } } if (MyInvocation.BoundParameters.ContainsKey("OMSFileLogLevel")) { switch (FileLogLevel.ToLower()) { case "off": fileAppender.Threshold = log4net.Core.Level.Off; break; case "info": fileAppender.Threshold = log4net.Core.Level.Info; break; case "warn": fileAppender.Threshold = log4net.Core.Level.Warn; break; case "error": fileAppender.Threshold = log4net.Core.Level.Error; break; case "debug": fileAppender.Threshold = log4net.Core.Level.Debug; break; default: fileAppender.Threshold = log4net.Core.Level.Info; break; } } fileAppender.ActivateOptions(); } #endregion #region ConsoleAppender if (logAppender.GetType() == typeof(log4net.Appender.ConsoleAppender)) { log4net.Appender.ConsoleAppender consoleAppender = (log4net.Appender.ConsoleAppender)logAppender; if (this.Host.Name.ToLower() == "consolehost") { if (MultiColor == false) { { if (MyInvocation.BoundParameters.ContainsKey("ConsoleLogLevel")) { switch (ConsoleLogLevel.ToLower()) { case "off": consoleAppender.Threshold = log4net.Core.Level.Off; break; case "info": consoleAppender.Threshold = log4net.Core.Level.Info; break; case "warn": consoleAppender.Threshold = log4net.Core.Level.Warn; break; case "error": consoleAppender.Threshold = log4net.Core.Level.Error; break; case "debug": consoleAppender.Threshold = log4net.Core.Level.Debug; break; default: consoleAppender.Threshold = log4net.Core.Level.Info; break; } } } } else { consoleAppender.Threshold = log4net.Core.Level.Off; } } else { if (this.Host.Name.ToLower() == "windows powershell ise host") { if (MyInvocation.BoundParameters.ContainsKey("ConsoleLogLevel")) { switch (ConsoleLogLevel.ToLower()) { case "off": consoleAppender.Threshold = log4net.Core.Level.Off; break; case "info": consoleAppender.Threshold = log4net.Core.Level.Info; break; case "warn": consoleAppender.Threshold = log4net.Core.Level.Warn; break; case "error": consoleAppender.Threshold = log4net.Core.Level.Error; break; case "debug": consoleAppender.Threshold = log4net.Core.Level.Debug; break; default: consoleAppender.Threshold = log4net.Core.Level.Info; break; } } if ((MyInvocation.BoundParameters.ContainsKey("MultiColor")) && (MultiColor == true)) { Console.WriteLine("Powershell ISe does not support multi colored displays."); } } } consoleAppender.ActivateOptions(); } #endregion #region coloredconsoleappender if (logAppender.GetType() == typeof(log4net.Appender.ColoredConsoleAppender)) { log4net.Appender.ColoredConsoleAppender coloredConsoleAppender = (log4net.Appender.ColoredConsoleAppender)logAppender; if (this.Host.Name.ToLower() == "consolehost") { if (MultiColor == true) { if (MyInvocation.BoundParameters.ContainsKey("ConsoleLogLevel")) { switch (ConsoleLogLevel.ToLower()) { case "off": coloredConsoleAppender.Threshold = log4net.Core.Level.Off; break; case "info": coloredConsoleAppender.Threshold = log4net.Core.Level.Info; break; case "warn": coloredConsoleAppender.Threshold = log4net.Core.Level.Warn; break; case "error": coloredConsoleAppender.Threshold = log4net.Core.Level.Error; break; case "debug": coloredConsoleAppender.Threshold = log4net.Core.Level.Debug; break; default: coloredConsoleAppender.Threshold = log4net.Core.Level.Info; break; } } } else { coloredConsoleAppender.Threshold = log4net.Core.Level.Off; } } if (this.Host.Name.ToLower() == "windows powershell ise host") { coloredConsoleAppender.Threshold = log4net.Core.Level.Off; } coloredConsoleAppender.ActivateOptions(); } #endregion } }
private static log4net.Appender.ConsoleAppender GetConsoleAppender( log4net.Core.Level consoleLogLevel) { log4net.Appender.ConsoleAppender appender = new log4net.Appender.ConsoleAppender(); appender.Name = "Console"; appender.Layout = new log4net.Layout.PatternLayout( "%d{yyyy-MM-dd HH:mm:ss} [%t] %-5p %c - %m%n"); appender.Threshold = consoleLogLevel; appender.ActivateOptions(); return appender; }