Example #1
0
        public Log4netLogger(JimuLog4netOptions options = null)
        {
            _options = options ?? new JimuLog4netOptions {
                EnableConsoleLog = true
            };
            string repositoryName = $"jimuLogger-{DateTime.Now}";
            var    repLogger      = LogManager.CreateRepository(repositoryName);

            //var repDebug = LogManager.CreateRepository("debug");
            //var repInfo = LogManager.CreateRepository("info");
            //var repWarn = LogManager.CreateRepository("warn");
            //var repError = LogManager.CreateRepository("error");
            //UseCodeConfig((Hierarchy)repDebug, "debug");
            //UseCodeConfig((Hierarchy)repInfo, "info");
            //UseCodeConfig((Hierarchy)repError, "error");
            //UseCodeConfig((Hierarchy)repWarn, "warn");
            UseCodeConfig((Hierarchy)repLogger, LogLevel.Debug);
            UseCodeConfig((Hierarchy)repLogger, LogLevel.Info);
            UseCodeConfig((Hierarchy)repLogger, LogLevel.Warn);
            UseCodeConfig((Hierarchy)repLogger, LogLevel.Error);
            _logger = LogManager.GetLogger(repositoryName, MethodBase.GetCurrentMethod().DeclaringType);
            //_logDebug = LogManager.GetLogger("debug", MethodBase.GetCurrentMethod().DeclaringType);
            //_logInfo = LogManager.GetLogger("info", MethodBase.GetCurrentMethod().DeclaringType);
            //_logError = LogManager.GetLogger("error", MethodBase.GetCurrentMethod().DeclaringType);
            //_logWarn = LogManager.GetLogger("warn", MethodBase.GetCurrentMethod().DeclaringType);
        }
Example #2
0
 public Log4netClientComponent(IConfigurationRoot jimuAppSettings) : base(jimuAppSettings)
 {
     _options = this.JimuAppSettings.GetSection(typeof(JimuLog4netOptions).Name).Get <JimuLog4netOptions>();
 }
Example #3
0
 public Log4netServerComponent(IConfigurationRoot jimuAppSettings) : base(jimuAppSettings)
 {
     _options = this.JimuAppSettings.GetSection("JimuLog4netOptions").Get <JimuLog4netOptions>();
 }
Example #4
0
        public NLogger(JimuLog4netOptions options = null)
        {
            options = options ?? new JimuLog4netOptions {
                EnableConsoleLog = true
            };
            var config = new NLog.Config.LoggingConfiguration();
            var ip     = JimuHelper.GetLocalIPAddress();

            if (options.EnableFileLog)
            {
                var fileConf = new NLog.Targets.FileTarget("jimuLogFile")
                {
                    FileName         = ".\\log\\${level:lowercase=true}\\${shortdate}.log",
                    ArchiveAboveSize = 10000000,
                    Layout           = @"${date:format=yyyy-MM-dd HH\:mm\:ss.fff} ${level:uppercase=true} [" + ip + "] ${message}"
                };
                if (options.FileLogPath != null)
                {
                    fileConf.FileName = options.FileLogPath + "\\${level:lowercase=true}\\${shortdate}.log";
                }
                if ((options.FileLogLevel & LogLevel.Error) == LogLevel.Error)
                {
                    config.AddRuleForOneLevel(NLog.LogLevel.Error, fileConf);
                }
                if ((options.FileLogLevel & LogLevel.Warn) == LogLevel.Warn)
                {
                    //config.AddRuleForOneLevel(NLog.LogLevel.Warn, fileConf);
                    config.AddRule(NLog.LogLevel.Warn, NLog.LogLevel.Error, fileConf);
                }
                if ((options.FileLogLevel & LogLevel.Info) == LogLevel.Info)
                {
                    config.AddRule(NLog.LogLevel.Info, NLog.LogLevel.Error, fileConf);
                    //config.AddRuleForOneLevel(NLog.LogLevel.Info, fileConf);
                }
                if ((options.FileLogLevel & LogLevel.Debug) == LogLevel.Debug)
                {
                    config.AddRule(NLog.LogLevel.Debug, NLog.LogLevel.Error, fileConf);
                    //config.AddRuleForOneLevel(NLog.LogLevel.Debug, fileConf);
                }
            }

            if (options.EnableConsoleLog)
            {
                var consoleLog = new NLog.Targets.ConsoleTarget("jimuLogconsole")
                {
                    Layout = @"${date:format=yyyy-MM-dd HH\:mm\:ss.fff} ${level:uppercase=true} [" + ip + "] ${message}"
                };
                if ((options.ConsoleLogLevel & LogLevel.Error) == LogLevel.Error)
                {
                    config.AddRuleForOneLevel(NLog.LogLevel.Error, consoleLog);
                }
                if ((options.ConsoleLogLevel & LogLevel.Warn) == LogLevel.Warn)
                {
                    config.AddRule(NLog.LogLevel.Warn, NLog.LogLevel.Error, consoleLog);
                    //config.AddRuleForOneLevel(NLog.LogLevel.Warn, consoleLog);
                }
                if ((options.ConsoleLogLevel & LogLevel.Info) == LogLevel.Info)
                {
                    config.AddRule(NLog.LogLevel.Info, NLog.LogLevel.Error, consoleLog);
                    //config.AddRuleForOneLevel(NLog.LogLevel.Info, consoleLog);
                }
                if ((options.ConsoleLogLevel & LogLevel.Debug) == LogLevel.Debug)
                {
                    config.AddRule(NLog.LogLevel.Debug, NLog.LogLevel.Error, consoleLog);
                    //config.AddRuleForOneLevel(NLog.LogLevel.Debug, consoleLog);
                }
            }
            NLog.LogManager.Configuration = config;
            _logger = NLog.LogManager.GetLogger("jimuLogger");
        }