Ejemplo n.º 1
0
 public static T UseLog4netLogger <T>(this IServiceHostBuilder serviceHostBuilder, LogOptions options = null)
     where T : class, IServiceHostBuilder
 {
     serviceHostBuilder.RegisterService(containerBuilder =>
     {
         if (options == null)
         {
             options = new LogOptions();
         }
         containerBuilder.RegisterType <Log4netLogger>().WithParameter("options", options).As <ILogger>().SingleInstance();
     });
     serviceHostBuilder.AddInitializer(container =>
     {
         var logger = container.Resolve <ILogger>();
         logger.Info($"[config]use Log4net logger");
     });
     return(serviceHostBuilder as T);
 }
Ejemplo n.º 2
0
        public NLogger(LogOptions options = null)
        {
            options = options ?? new LogOptions {
                EnableConsoleLog = true
            };
            var config = new NLog.Config.LoggingConfiguration();

            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}  ${message}"
                };
                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}  ${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");
        }