Exemple #1
0
        private static Logger SetupNLog(NLogConfigurationApi config)
        {
            var logDirectory = string.IsNullOrEmpty(config.LogDirectory) ?
                "${basedir}/Logs" :
                config.LogDirectory;

            var fileTarget = new FileTarget
            {
                Name = "FileTarget",
                Layout = "${message}",
                ConcurrentWrites = false,
                FileName = new SimpleLayout(Path.Combine(logDirectory, "current.log")),
                ArchiveEvery = config.ArchivePeriod,
                ArchiveNumbering = ArchiveNumberingMode.Sequence,
                MaxArchiveFiles = config.MaxArchiveFiles,
                ArchiveFileName = new SimpleLayout(Path.Combine(logDirectory,"archive/{####}.log"))
            };
            var asyncWrapper = new AsyncTargetWrapper(fileTarget)
            {
                Name = "AsyncWrapper"
            };

            var loggingConfiguration = new LoggingConfiguration();
            loggingConfiguration.AddTarget(LoggerName, asyncWrapper);
            loggingConfiguration.LoggingRules.Add(new LoggingRule("*", LevelToNLogLevel(config.MinimumLogLevel), asyncWrapper));

            LogManager.Configuration = loggingConfiguration;

            return LogManager.GetLogger(LoggerName);
        }
Exemple #2
0
        public static void Initialize(Action <NLogConfigurationApi> configure)
        {
            if (_logger != null)
            {
                return;
            }

            var config = new NLogConfigurationApi();

            configure(config);

            _logger = SetupNLog(config);

            Behavior.UseCustomLogging((level, message) =>
            {
                _logger.Log(level.ToNLogLevel(), message);
            });
        }
Exemple #3
0
        public static void Initialize(Action<NLogConfigurationApi> configure = null)
        {
            if (_logger != null)
                return;

            var config = new NLogConfigurationApi();
            if (configure != null)
            {
                configure(config);
            }

            _logger = SetupNLog(config);

            LoggingFacade.Initialize((level, message) =>
            {
                var nLogLevel = LevelToNLogLevel(level);
                _logger.Log(nLogLevel, message);
            });
        }
Exemple #4
0
        static Logger SetupNLog(NLogConfigurationApi config)
        {
            var targets = new List <Target>();

            if (config.TargetsConfiguration == null)
            {
                throw new NotSupportedException("Need to configure 'Targets'");
            }

            var file = config.TargetsConfiguration.FileConfiguration;

            if (file != null)
            {
                var logDirectory = string.IsNullOrEmpty(file.LogDirectory) ? "${basedir}/Logs" : file.LogDirectory;

                targets.Add(new FileTarget
                {
                    Name             = "FileTarget",
                    Layout           = "${message}",
                    ConcurrentWrites = false,
                    FileName         = new SimpleLayout(Path.Combine(logDirectory, "current.log")),
                    ArchiveEvery     = file.ArchivePeriod,
                    ArchiveNumbering = ArchiveNumberingMode.Sequence,
                    MaxArchiveFiles  = file.MaxArchiveFiles,
                    ArchiveFileName  = new SimpleLayout(Path.Combine(logDirectory, "archive/{####}.log"))
                });
            }

            var coloredConsole = config.TargetsConfiguration.ColoredConsoleConfiguration;

            if (coloredConsole != null)
            {
                targets.Add(new ColoredConsoleTarget());
            }

            var network = config.TargetsConfiguration.NetworkConfiguration;

            if (network != null)
            {
                targets.Add(new NetworkTarget
                {
                    Address = network.Address
                });
            }

            if (targets.Count == 0)
            {
                throw new NotSupportedException("Need to specify at least 1 target (e.g. File/ColoredConsole)");
            }

            var target = targets.Count == 1 ? targets.Single() : new SplitGroupTarget(targets.ToArray());

            var asyncWrapper = new AsyncTargetWrapper(target)
            {
                Name = "AsyncWrapper"
            };

            var loggingConfiguration = new LoggingConfiguration();

            loggingConfiguration.AddTarget(LoggerName, asyncWrapper);
            loggingConfiguration.LoggingRules.Add(new LoggingRule("*", config.MinimumLogLevel.ToNLogLevel(), asyncWrapper));

            LogManager.Configuration = loggingConfiguration;

            return(LogManager.GetLogger(LoggerName));
        }