Exemple #1
0
        public static void ConfigureLogger()
        {
            var config = new LoggingConfiguration();

            var consoleTarget = new ColoredConsoleTarget("target1")
            {
                Layout = @"${date:format=HH\:mm\:ss} ${level} ${message} ${exception}"
            };


            var infoLog = new FileTarget("infoLog")
            {
                FileName = Globals.LogTextFile,
                Layout   = "${longdate}\n${level}\n${message}\n${exception} \n"
            };

            var errorLog = new FileTarget("errorLog")
            {
                FileName = Globals.ErrorLogTextFile,
                Layout   = "${longdate}\n${level}\n${message}\n${exception} \n"
            };

            config.AddTarget(infoLog);
            config.AddTarget(errorLog);
            config.AddTarget(consoleTarget);


            config.AddRuleForAllLevels(consoleTarget); // all to console
            config.AddRuleForAllLevels(infoLog);
            config.AddRuleForOneLevel(LogLevel.Error, errorLog);
            config.AddRuleForOneLevel(LogLevel.Fatal, errorLog);

            LogManager.Configuration = config;
        }
Exemple #2
0
        static Log()
        {
            var config        = new LoggingConfiguration();
            var consoleTarget = new ColoredConsoleTarget("target1")
            {
                Layout = @"${date:format=HH\:mm\:ss} | ${level} | ${message} | ${exception}"
            };

            config.AddTarget(consoleTarget);

            var errorTarget = new FileTarget("ErrorLog")
            {
                FileName = "${basedir}/logs/${shortdate}.log",
                Layout   = "${longdate} | ${level} | Message: ${message} | Exception: ${exception:format=ToString,Stacktrace}${newline}"
            };

            var infoTarget = new FileTarget("InfoTarget")
            {
                FileName = "${basedir}/logs/${shortdate}.log",
                Layout   = "${longdate} | ${level} | Message: ${message}"
            };

            var errorWrapper = new AsyncTargetWrapper(errorTarget, 5000, AsyncTargetWrapperOverflowAction.Discard);
            var infoWrapper  = new AsyncTargetWrapper(infoTarget, 5000, AsyncTargetWrapperOverflowAction.Discard);

            config.AddTarget("ErrorWrapper", errorWrapper);
            config.AddTarget("InfoWrapper", infoWrapper);

            config.AddRuleForOneLevel(LogLevel.Info, "InfoWrapper");
            config.AddRuleForOneLevel(LogLevel.Error, "ErrorWrapper");
            config.AddRuleForOneLevel(LogLevel.Debug, "InfoWrapper");
            config.AddRuleForAllLevels(consoleTarget);

            LogManager.Configuration = config;
        }
Exemple #3
0
        private void ConfigureLogging(IServiceCollection services)
        {
            var config = new LoggingConfiguration();
            var target = new LogListener();

            config.AddTarget("LogListener", target);
            config.AddRuleForOneLevel(NLog.LogLevel.Error, target);
            config.AddRuleForOneLevel(NLog.LogLevel.Info, target);
            LogManager.Configuration = config;
            services.AddLogging(cfg => cfg.AddNLog());
            services.AddSingleton <LogListener>(target);
        }
Exemple #4
0
        static void SetupLogging(bool logToConsole)
        {
            // Step 1. Create configuration object
            var config = new LoggingConfiguration();

            // Step 2. Create targets
            var consoleTarget = new ColoredConsoleTarget("console")
            {
                Layout = @"[${date:format=HH\:mm\:ss}] ${level}: ${message} ${exception}"
            };

            config.AddTarget(consoleTarget);

            var fileTarget = new FileTarget("errorfile")
            {
                FileName = "${basedir}/Tarzan.Nfx.PcapLoader.err",
                Layout   = "[${longdate}] ${level}: ${message}  ${exception}"
            };

            config.AddTarget(fileTarget);

            config.AddRuleForOneLevel(LogLevel.Error, fileTarget);
            // Step 3. Define rules
            if (logToConsole)
            {
                config.AddRule(LogLevel.Info, LogLevel.Fatal, consoleTarget);
            }

            // Step 4. Activate the configuration
            LogManager.Configuration = config;
        }
        public static void LogHelp()
        {
            // Step 1. Create configuration object
            var config = new LoggingConfiguration();

            // Step 2. Create targets
            var consoleTarget = new ColoredConsoleTarget("target1")
            {
                Layout = @"${date:format=HH\:mm\:ss} ${level} ${message} ${exception}"
            };

            config.AddTarget(consoleTarget);

            var fileTarget = new FileTarget("target2")
            {
                FileName = "${basedir}/file.txt",
                Layout   = "${longdate} ${level} ${message}  ${exception}"
            };

            config.AddTarget(fileTarget);


            // Step 3. Define rules
            config.AddRuleForOneLevel(LogLevel.Error, fileTarget); // only errors to file
            config.AddRuleForAllLevels(consoleTarget);             // all to console

            // Step 4. Activate the configuration
            LogManager.Configuration = config;
        }
        static Logger Logging()
        {
            // Step 1. Create configuration object
            var config = new LoggingConfiguration();

            // Step 2. Create targets
            var consoleTarget = new ColoredConsoleTarget("console")
            {
                Layout = @"${date:format=HH\:mm\:ss} ${level} ${message} ${exception}"
            };

            config.AddTarget(consoleTarget);

            var fileTarget = new FileTarget("filelogs")
            {
                FileName = "${basedir}/iceberg.log",
                Layout   = "${longdate} ${level} ${message}  ${exception}"
            };

            config.AddTarget(fileTarget);

            // Step 3. Define rules
            config.AddRuleForOneLevel(LogLevel.Error, fileTarget); // only errors to file
            config.AddRuleForAllLevels(consoleTarget);             // all to console

            // Step 4. Activate the configuration
            LogManager.Configuration = config;

            return(LogManager.GetLogger("Iceberg"));
        }
Exemple #7
0
        static void SetupLogger()
        {
            var config     = new LoggingConfiguration();
            var fileTarget = new FileTarget("file")
            {
                Layout           = @"[${longdate}][${level}] ${message} ${exception:format=message}",
                FileName         = Path.Combine(Resources.LogFolder, "error.log"),
                ArchiveEvery     = FileArchivePeriod.Day,
                ArchiveAboveSize = 10240,
                ArchiveNumbering = ArchiveNumberingMode.Date,
            };
            var consoleTarget = new ColoredConsoleTarget("console")
            {
                Layout = @"[${date:format=HH\:mm\:ss}][${level}] ${message} ${exception:format=toString}",
            };

            config.AddTarget(fileTarget);
            config.AddTarget(consoleTarget);

            // Only allow logs more severe or equally severe as Warning to file
            config.AddRuleForOneLevel(LogLevel.Warn, fileTarget);
            // Allow all logs to console
            config.AddRuleForAllLevels(consoleTarget);

            // Activate configuration
            LogManager.Configuration = config;
        }
Exemple #8
0
        private static void InitLogger()
        {
            // Step 1. Create configuration object
            var config = new LoggingConfiguration();

            // Step 2. Create targets
            var consoleTarget = new ColoredConsoleTarget("consoleTarget")
            {
                Layout = @"${date:format=HH\:mm\:ss} ${level} ${message} ${exception}"
            };

            config.AddTarget(consoleTarget);

            var fileTarget = new FileTarget("fileTarget")
            {
                FileName = Path.Combine(_podcastConfig.LogFolder, "podcast.log"),
                Layout   = "${longdate} ${level} ${message}  ${exception}"
            };

            config.AddTarget(fileTarget);

            // Step 3. Define rules
            config.AddRuleForOneLevel(LogLevel.Info, fileTarget); // only errors to file
            config.AddRuleForAllLevels(consoleTarget);            // all to console

            // Step 4. Activate the configuration
            LogManager.Configuration = config;

            _logger = LogManager.GetLogger("PodcastLogger");
        }
Exemple #9
0
        protected override void Load(ContainerBuilder builder)
        {
            // Create config
            var config = new LoggingConfiguration();

            // EventLog
            string productName    = Assembly.GetCallingAssembly().GetName().Name;
            var    eventLogTarget = new EventLogTarget(productName)
            {
                EntryType     = "EventLog"
                , Layout      = "${longdate} ${level} ${message}  ${exception}"
                , Category    = "Layout"
                , EventId     = "Layout"
                , MachineName = Environment.MachineName
            };

            // File Log
            var fileTarget = new FileTarget("fileTarget")
            {
                FileName = "${basedir}/file.txt",
                Layout   = "${longdate} ${level} ${message}  ${exception}"
            };

            config.AddTarget(fileTarget);

            // Define rules
            config.AddRuleForOneLevel(LogLevel.Warn, fileTarget);
            config.AddRule(LogLevel.Info, LogLevel.Fatal, eventLogTarget);

            // Activate the configuration
            LogManager.Configuration = config;
        }
Exemple #10
0
        private void ConfigureLogging()
        {
            var config = new LoggingConfiguration();

            var fileTarget    = CreateFileTarget();
            var consoleTarget = CreateConsoleTarget();

            config.AddTarget(fileTarget);
            config.AddTarget(consoleTarget);

            config.AddRuleForOneLevel(LogLevel.Debug, consoleTarget);
            config.AddRuleForOneLevel(LogLevel.Info, consoleTarget);
            config.AddRuleForOneLevel(LogLevel.Warn, consoleTarget);
            config.AddRuleForOneLevel(LogLevel.Error, consoleTarget);
            config.AddRuleForOneLevel(LogLevel.Fatal, consoleTarget);

            LogManager.Configuration = config;
        }
Exemple #11
0
        protected void InitLogger()
        {
            var config     = new LoggingConfiguration();
            var fileTarget = new FileTarget("target2")
            {
                ArchiveNumbering = ArchiveNumberingMode.DateAndSequence,
                ArchiveAboveSize = 10485760,
                FileName         = BackTestPath + "/logs/" + this.SymbolName + "_" + this.FileName + (this.RunningMode == RunningMode.RealTime ? "" : "_backtest") + ".txt",
                Layout           = "${longdate} ${callsite:className=True:fileName=False:includeSourcePath=False:methodName=False} ${level} ${message} ${exception:format=ToString,StackTrace} ${stacktrace:format=DetailedFlat:topFrames=5:skipFrames=5:separator=&#13;&#10;}"
            };

            config.AddTarget(fileTarget);
            config.AddRuleForOneLevel(LogLevel.Debug, fileTarget);
            config.AddRuleForOneLevel(LogLevel.Info, fileTarget);
            config.AddRuleForOneLevel(LogLevel.Warn, fileTarget);
            config.AddRuleForOneLevel(LogLevel.Error, fileTarget);
            LogManager.Configuration = config;
        }
        public Logger ConfigureLogger()
        {
            var config = new LoggingConfiguration();

            var consoleTarget = new ColoredConsoleTarget()
            {
                Name   = "console",
                Layout = "${date}|${level:uppercase=true}|${message} ${exception}|${logger}|${all-event-properties}"
            };

            config.AddTarget(consoleTarget);
            config.AddRuleForAllLevels(consoleTarget);

            var fileTarget = new FileTarget()
            {
                Name     = "localerrorfile",
                FileName = "${currentdir}/errors.log",
                Layout   = "${date}|${level:uppercase=true}|${message} ${exception}|${logger}|${all-event-properties}"
            };

            config.AddTarget(fileTarget);
            config.AddRuleForOneLevel(LogLevel.Error, fileTarget);

            if (!string.IsNullOrWhiteSpace(_configuration.GetValue <string>("SlackWebhookUrl")))
            {
                var slackTarget = new SlackTarget()
                {
                    Name       = "slack",
                    WebHookUrl = _configuration.GetValue <string>("SlackWebhookUrl"),
                    Layout     = "[${level:uppercase=true}]\n${message}\n${exception}"
                                 // Add Fields and such
                };
                slackTarget.Compact = true;
                config.AddTarget(slackTarget);
                config.AddRuleForOneLevel(LogLevel.Error, slackTarget);
            }

            InternalLogger.LogFile     = "internal-nlog.txt";
            InternalLogger.LogLevel    = LogLevel.Info;
            LogManager.ThrowExceptions = true;
            LogManager.Configuration   = config;

            return(LogManager.GetCurrentClassLogger());
        }
Exemple #13
0
        public static int Main(string[] args)
        {
            bool   autoAccept  = true;
            string endpointURL = "opc.tcp://*****:*****@"${date:format=HH\:mm\:ss} ${message} ${exception}"
            };

            config.AddTarget(consoleTarget);

            var fileTarget = new FileTarget("errorTarget")
            {
                FileName = "${basedir}/logs/error.txt",
                Layout   = "${longdate} - ${level}: ${message} ${exception}"
            };

            config.AddTarget(fileTarget);

            var fileEventTarget = new FileTarget("fileTarget")
            {
                FileName = "${basedir}/logs/event.txt",
                Layout   = "${longdate} - ${level}: ${message}"
            };

            config.AddRuleForOneLevel(LogLevel.Error, fileTarget);
            config.AddRuleForOneLevel(LogLevel.Info, fileEventTarget);
            config.AddRuleForAllLevels(consoleTarget);
            LogManager.Configuration = config;

            Logger logger = LogManager.GetLogger("OPCuaKashira");

            logger.Info("Start OPC UA Kashira Clients");
            KashiraClient client = new KashiraClient(_endpointURL: endpointURL, _autoAccept: autoAccept);

            client.Run();
            return((int)KashiraClient.ExitCode);
        }
Exemple #14
0
        private static void ActivateDebugLogging()
        {
            var config     = new LoggingConfiguration();
            var logconsole = new ConsoleTarget("logconsole")
            {
                Layout = @"${level}: ${message} ${exception}"
            };

            config.AddTarget(logconsole);
            config.AddRuleForOneLevel(LogLevel.Debug, logconsole);
            LogManager.Configuration = config;
        }
Exemple #15
0
 /// <summary>
 /// Enable log levels according to environment variables
 /// </summary>
 /// <param name="config">logging configuration</param>
 /// <param name="logEnabled">force enable logs</param>
 public static void Enable(LoggingConfiguration config, string logEnabled = default)
 {
     foreach (var target in config.AllTargets)
     {
         if (Environment.GetEnvironmentVariable("TRACE") == "1" || (logEnabled?.Contains("TRACE") ?? false))
         {
             config.AddRuleForOneLevel(LogLevel.Trace, target);
         }
         if (Environment.GetEnvironmentVariable("DEBUG") == "1" || (logEnabled?.Contains("DEBUG") ?? false))
         {
             config.AddRuleForOneLevel(LogLevel.Debug, target);
         }
         if (Environment.GetEnvironmentVariable("ERROR") == "1" || (logEnabled?.Contains("ERROR") ?? false))
         {
             config.AddRuleForOneLevel(LogLevel.Error, target);
         }
         if (Environment.GetEnvironmentVariable("INFO") == "1" || (logEnabled?.Contains("INFO") ?? false))
         {
             config.AddRuleForOneLevel(LogLevel.Info, target);
         }
         if (Environment.GetEnvironmentVariable("WARN") == "1" || (logEnabled?.Contains("WARN") ?? false))
         {
             config.AddRuleForOneLevel(LogLevel.Warn, target);
         }
         if (Environment.GetEnvironmentVariable("FATAL") == "1" || (logEnabled?.Contains("FATAL") ?? false))
         {
             config.AddRuleForOneLevel(LogLevel.Fatal, target);
         }
     }
 }
        public PrescreenController()
        {
            var fileTarget = new FileTarget("target2")
            {
                FileName = "${basedir}/file_prescreening.txt",
                Layout   = "${longdate} ${level} ${message}  ${exception}"
            };

            config.AddTarget(fileTarget);
            config.AddRuleForOneLevel(LogLevel.Debug, fileTarget); // only errors to file
            LogManager.Configuration = config;
            logger = LogManager.GetLogger("AlexaAPILog");
        }
Exemple #17
0
        /// <summary>API Entry Point</summary>
        public static void Main(string[] args)
        {
            var config = new LoggingConfiguration();

            // targets
            var fileTarget = new FileTarget("fileTarget")
            {
                FileName = @"D:\Users\Dave\Documents\Visual Studio 2019\Projects\DbWebAPI\logs\DbWebAPIlog-${shortdate}.log",
                Layout   = "${longdate}|${event-properties:item=EventId_Id}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring}"
            };

            config.AddTarget(fileTarget);
            // rules
            config.AddRuleForOneLevel(NLog.LogLevel.Warn, fileTarget);
            config.AddRuleForOneLevel(NLog.LogLevel.Error, fileTarget);
            config.AddRuleForOneLevel(NLog.LogLevel.Fatal, fileTarget);
            LogManager.Configuration = config;

            //MessageHandler.DebugLog("Starting", true);

            CreateHostBuilder(args).Build().Run();
        }
        static void Main(string[] args)
        {
            // Step 1. Create configuration object
            var config = new LoggingConfiguration();

            // Step 2. Create targets
            var consoleTarget = new ColoredConsoleTarget("target1")
            {
                Layout = @"${date:format=HH\:mm\:ss} ${level} ${message} ${exception}"
            };

            config.AddTarget(consoleTarget);

            var fileTarget = new FileTarget("target2")
            {
                FileName = "${basedir}/file.txt",
                Layout   = "${longdate} ${level} ${message}  ${exception}"
            };

            config.AddTarget(fileTarget);


            // Step 3. Define rules
            config.AddRuleForOneLevel(LogLevel.Error, fileTarget); // only errors to file
            config.AddRuleForAllLevels(consoleTarget);             // all to console

            // Step 4. Activate the configuration
            LogManager.Configuration = config;

            // Example usage
            Logger logger = LogManager.GetLogger("Example");

            logger.Trace("trace log message");
            logger.Debug("debug log message");
            logger.Info("info log message");
            logger.Warn("warn log message");
            logger.Error("error log message");
            logger.Fatal("fatal log message");

            //Example of logging exceptions

            try
            {
            }
            catch (Exception ex)
            {
                logger.Error(ex, "ow noos!");
                throw;
            }
        }
Exemple #19
0
        public RequestValidatorMiddleware(RequestDelegate next)
        {
            _next = next;
            var fileTarget = new FileTarget("target2")
            {
                FileName = "${basedir}/file.txt",
                Layout   = "${longdate} ${level} ${message}  ${exception}"
            };

            config.AddTarget(fileTarget);
            config.AddRuleForOneLevel(LogLevel.Debug, fileTarget); // only errors to file
            LogManager.Configuration = config;
            logger = LogManager.GetLogger("AlexaAPILog");
        }
        public void AddNLogRule(LogLevel level, Target target, string loggerNamePattern, bool final = false)
        {
            if ("*".Equals(loggerNamePattern))
            {
                throw new ArgumentException("loggerNamePattern cannot be *");
            }

            var targetGroup = new FallbackGroupTarget(target, _failedDefaultTarget)
            {
                ReturnToFirstOnSuccess = true
            };

            _config.AddRuleForOneLevel(level, new AsyncTargetWrapper(targetGroup), loggerNamePattern, final);
        }
Exemple #21
0
        public static LoggingConfiguration CreateLoggingConfiguration()
        {
            var loggerConfiguration = new LoggingConfiguration();

            loggerConfiguration.AddTarget(new FileTarget($"BotFileTarget")
            {
                FileName = Path.Combine(Directory.GetCurrentDirectory(), "Logs", "BotLogs.txt"),
                Layout   = "${longdate} -- ${level} -- ${message} -- ${exception} --"
            });

            loggerConfiguration.AddTarget(new ColoredConsoleTarget("BotConsoleTarget")
            {
                Layout = @"${date:format=HH\:mm\:ss} -- ${level} -- ${message} -- ${exception} --"
            });

            loggerConfiguration.AddRuleForOneLevel(LogLevel.Warn, "BotFileTarget");
            loggerConfiguration.AddRuleForOneLevel(LogLevel.Error, "BotFileTarget");
            loggerConfiguration.AddRuleForOneLevel(LogLevel.Fatal, "BotFileTarget");

            loggerConfiguration.AddRuleForAllLevels("BotConsoleTarget");

            return(loggerConfiguration);
        }
Exemple #22
0
        /// <summary>
        /// <para>en: Initialization configuration for writing to file </para>
        /// <para>vi: Cấu hình khởi tạo để ghi vào tập tin</para>
        /// </summary>
        public static void Initialization(bool Info = true, bool Debug = true, bool Warn = true, bool Error = true, bool Trace = true, bool Off = true, bool Fatal = true)
        {
            LoggingConfiguration config = new LoggingConfiguration();
            //config for console log
            ColoredConsoleTarget consoleTarget = new ColoredConsoleTarget("ConsoleTarget")
            {
                Layout = "${longdate}|${level}|${message}${callsite:className=false:fileName=true:includeSourcePath=false:methodName=false}|${exception}"
            };

            config.AddTarget(consoleTarget);
            //config for file log
            FileTarget fileTarget = new FileTarget("FileTarget")
            {
                FileName = "${basedir}/logs/${date:format=yyyy_MM_dd}.log",
                Layout   = "${longdate}|${level}|${message}${callsite:className=false:fileName=true:includeSourcePath=false:methodName=false}|${exception}"
            };

            config.AddTarget(fileTarget);
            // errors and warning write to file
            if (Info)
            {
                config.AddRuleForOneLevel(LogLevel.Info, fileTarget);
            }
            if (Error)
            {
                config.AddRuleForOneLevel(LogLevel.Error, fileTarget);
            }
            if (Warn)
            {
                config.AddRuleForOneLevel(LogLevel.Warn, fileTarget);
            }
            if (Debug)
            {
                config.AddRuleForOneLevel(LogLevel.Debug, fileTarget);
            }
            if (Fatal)
            {
                config.AddRuleForOneLevel(LogLevel.Fatal, fileTarget);
            }
            if (Off)
            {
                config.AddRuleForOneLevel(LogLevel.Off, fileTarget);
            }
            if (Trace)
            {
                config.AddRuleForOneLevel(LogLevel.Trace, fileTarget);
            }
            // all to console
            config.AddRuleForAllLevels(consoleTarget);
            //Activate the configuration
            LogManager.Configuration = config;
        }
Exemple #23
0
        /// <summary>
        ///     <inheritdoc/>
        /// </summary>
        public void UpdateConfigurationLogLevel()
        {
            LoggingConfiguration.LoggingRules.Clear();
            switch ((LogLevel)SoftwareSettings.Settings.GetSettings.GetLogLevel)
            {
            case LogLevel.All:
            {
                LoggingConfiguration.AddRuleForOneLevel(NLog.LogLevel.Info, Target);
                LoggingConfiguration.AddRuleForOneLevel(NLog.LogLevel.Trace, Target);
                LoggingConfiguration.AddRuleForOneLevel(NLog.LogLevel.Warn, Target);
                LoggingConfiguration.AddRuleForOneLevel(NLog.LogLevel.Error, Target);
                LoggingConfiguration.AddRuleForOneLevel(NLog.LogLevel.Fatal, Target);
                break;
            }

            case LogLevel.Main:
            {
                LoggingConfiguration.AddRuleForOneLevel(NLog.LogLevel.Info, Target);
                LoggingConfiguration.AddRuleForOneLevel(NLog.LogLevel.Warn, Target);
                LoggingConfiguration.AddRuleForOneLevel(NLog.LogLevel.Error, Target);
                LoggingConfiguration.AddRuleForOneLevel(NLog.LogLevel.Fatal, Target);
                break;
            }

            case LogLevel.Error:
            {
                LoggingConfiguration.AddRuleForOneLevel(NLog.LogLevel.Info, Target);
                LoggingConfiguration.AddRuleForOneLevel(NLog.LogLevel.Debug, Target);
                LoggingConfiguration.AddRuleForOneLevel(NLog.LogLevel.Warn, Target);
                LoggingConfiguration.AddRuleForOneLevel(NLog.LogLevel.Error, Target);
                LoggingConfiguration.AddRuleForOneLevel(NLog.LogLevel.Fatal, Target);
                break;
            }

            case LogLevel.Debug:
            {
                LoggingConfiguration.AddRuleForAllLevels(Target);
                break;
            }

            default:
                throw new ArgumentOutOfRangeException();
            }

            LogManager.ReconfigExistingLoggers();
        }
Exemple #24
0
        private static void ConfigurationLogger()
        {
            var config        = new LoggingConfiguration();
            var consoleTarget = new ColoredConsoleTarget("target1")
            {
                Layout = @"${date:format=HH\:mm\:ss} ${message} ${exception}"
            };

            config.AddTarget(consoleTarget);

            var fileTargetWhiteNumber = new FileTarget("target2")
            {
                FileName = "${basedir}/logs/${shortdate} good number.txt",
                Layout   = "${longdate} ${message}  ${exception}"
            };

            config.AddTarget(fileTargetWhiteNumber);

            var fileTargetBlackNumber = new FileTarget("target3")
            {
                FileName = "${basedir}/logs/${shortdate} spam number.txt",
                Layout   = "${longdate} ${message}  ${exception}"
            };

            config.AddTarget(fileTargetBlackNumber);

            var fileTargetAll = new FileTarget("target4")
            {
                FileName = "${basedir}/logs/${shortdate} all.txt",
                Layout   = "${longdate} ${message}  ${exception}"
            };

            config.AddTarget(fileTargetAll);

            var fileTargetFatalError = new FileTarget("target5")
            {
                FileName = "${basedir}/logs/${shortdate} error.txt",
                Layout   = "${longdate} ${message}  ${exception}"
            };

            config.AddTarget(fileTargetFatalError);


            config.AddRuleForOneLevel(LogLevel.Info, fileTargetBlackNumber, "3CXSpamCallBlocker");
            config.AddRuleForOneLevel(LogLevel.Error, fileTargetWhiteNumber, "3CXSpamCallBlocker");
            config.AddRuleForOneLevel(LogLevel.Fatal, fileTargetFatalError, "3CXSpamCallBlocker");
            config.AddRuleForAllLevels(fileTargetAll, "3CXSpamCallBlocker");
            config.AddRuleForOneLevel(LogLevel.Info, consoleTarget, "3CXSpamCallBlocker");
            config.AddRuleForOneLevel(LogLevel.Error, consoleTarget, "3CXSpamCallBlocker");
            config.AddRuleForOneLevel(LogLevel.Fatal, consoleTarget, "3CXSpamCallBlocker");

            LogManager.Configuration = config;
        }
        /// <summary>
        /// Inicialize the cambright
        /// </summary>
        public override void Initialize()
        {
            nLoggerConfiguration = LightConfigurator.Config <NLoggerConfiguration>("NLogger");

            EnabledLogTrafic    = nLoggerConfiguration.EnabledLogTrafic;
            isRegistryTelemetry = nLoggerConfiguration.IntegratedTelemetry;

            LoggingConfiguration config = new LoggingConfiguration();
            // Step 2. Create targets
            var consoleTarget = new ColoredConsoleTarget("default")
            {
                Layout = @"${date:format=HH\:mm\:ss} ${level} ${message} ${exception}"
            };

            config.AddTarget(consoleTarget);

            var fileTarget = new FileTarget("onFile")
            {
                Layout          = nLoggerConfiguration.Layout,
                FileName        = nLoggerConfiguration.FileName,
                ArchiveFileName = nLoggerConfiguration.FileName + "-{##}.log",

                FileNameKind           = FilePathKind.Relative,
                FileAttributes         = Win32FileAttributes.Normal,
                CreateDirs             = true,
                ArchiveAboveSize       = 102400,
                ArchiveNumbering       = ArchiveNumberingMode.Sequence,
                MaxArchiveFiles        = 100,
                ConcurrentWrites       = true,
                KeepFileOpen           = true,
                DeleteOldFileOnStartup = true
            };

            config.AddTarget(fileTarget);

            // Step 3. Define rules
            config.AddRuleForOneLevel(NLog.LogLevel.FromString(nLoggerConfiguration.LogLevel), fileTarget); // only errors to file
            config.AddRuleForAllLevels(consoleTarget);                                                      // all to console

            // Step 4. Activate the configuration
            LogManager.Configuration = config;
            logger = LogManager.GetLogger("NLogger");
        }
Exemple #26
0
        public void CheckAllTargets()
        {
            var config     = new LoggingConfiguration();
            var fileTarget = new FileTarget {
                Name = "File", FileName = "file"
            };

            config.AddRuleForOneLevel(LogLevel.Error, fileTarget, "*a");

            config.AddTarget(fileTarget);

            Assert.Single(config.AllTargets);
            Assert.Equal(fileTarget, config.AllTargets[0]);

            config.InitializeAll();

            Assert.Single(config.AllTargets);
            Assert.Equal(fileTarget, config.AllTargets[0]);
        }
Exemple #27
0
        public void AddRule_with_target()
        {
            var config     = new LoggingConfiguration();
            var fileTarget = new FileTarget {
                Name = "File"
            };

            config.AddRuleForOneLevel(LogLevel.Error, fileTarget, "*a");
            Assert.NotNull(config.LoggingRules);
            Assert.Equal(1, config.LoggingRules.Count);
            config.AddTarget(new FileTarget {
                Name = "File"
            });
            var allTargets = config.AllTargets;

            Assert.NotNull(allTargets);
            Assert.Equal(1, allTargets.Count);
            Assert.Equal("File", allTargets.First().Name);
            Assert.NotNull(config.FindTargetByName <FileTarget>("File"));
        }
Exemple #28
0
        public NlogerConfig()
        {
            var config     = new LoggingConfiguration();
            var filetarget = new FileTarget("filetarget")
            {
                FileName = "${basedir}/ErrorLog/ErrorLog ${shortdate}.txt",
                Layout   = @"${longdate} ${newLine} ${level} ${newline} Exception ${exception} ${newline} Message ${message} ${newline} ${StackTrace} ${newline}"
            };

            filetarget.ArchiveEvery         = FileArchivePeriod.Day;
            filetarget.ArchiveFileName      = "${basedir}/LogArchive/${shortdate}/ErrorlogArchive.{#}.log";
            filetarget.ArchiveNumbering     = ArchiveNumberingMode.DateAndSequence;
            filetarget.CreateDirs           = true;
            filetarget.ConcurrentWrites     = true;
            filetarget.OpenFileCacheTimeout = 30;
            filetarget.ArchiveAboveSize     = 10240;
            config.AddTarget(filetarget);
            config.AddRuleForOneLevel(LogLevel.Error, filetarget);
            LogManager.Configuration = config;
        }
Exemple #29
0
        public static void ConfigureSimpleFileLogger(ThreadingType threadingType)
        {
            var logFileName = $"{Parameters.RootLogsDirectory}\\NLog.{threadingType}.SimpleFile.log";

            File.Delete(logFileName);

            var config = new LoggingConfiguration();

            var simpleLogFileTarget = new FileTarget("SimpleLogFileTarget")
            {
                FileName = logFileName,
                Layout   = LogOutputTemplate,

                KeepFileOpen = true, // Improves performance drastically (by default is set to false)
            };

            config.AddTarget(simpleLogFileTarget);

            config.AddRuleForOneLevel(LogLevel.Info, simpleLogFileTarget);

            LogManager.Configuration = config;
        }
Exemple #30
0
        public void AddRule_onelevel()
        {
            var config = new LoggingConfiguration();

            config.AddTarget(new FileTarget {
                Name = "File"
            });
            config.AddRuleForOneLevel(LogLevel.Error, "File", "*a");
            Assert.NotNull(config.LoggingRules);
            Assert.Equal(1, config.LoggingRules.Count);
            var rule1 = config.LoggingRules.FirstOrDefault();

            Assert.NotNull(rule1);
            Assert.Equal(false, rule1.Final);
            Assert.Equal("*a", rule1.LoggerNamePattern);
            Assert.Equal(false, rule1.IsLoggingEnabledForLevel(LogLevel.Fatal));
            Assert.Equal(true, rule1.IsLoggingEnabledForLevel(LogLevel.Error));
            Assert.Equal(false, rule1.IsLoggingEnabledForLevel(LogLevel.Warn));
            Assert.Equal(false, rule1.IsLoggingEnabledForLevel(LogLevel.Info));
            Assert.Equal(false, rule1.IsLoggingEnabledForLevel(LogLevel.Debug));
            Assert.Equal(false, rule1.IsLoggingEnabledForLevel(LogLevel.Trace));
            Assert.Equal(false, rule1.IsLoggingEnabledForLevel(LogLevel.Off));
        }