Exemplo n.º 1
0
        public NLogWrapper(string name=null)
        {
            if (string.IsNullOrWhiteSpace(name))
                name = "logger";

            _loggerImplementation = NLogFactory.Create(name);
        }
        public override ILogger Create(string name)
        {
            if (name == null)
            {
                throw new ArgumentNullException(nameof(name));
            }

            return(new CastleNlogLogger(nLogFactory.Create(name), this));
        }
Exemplo n.º 3
0
        public void Should_preserve_correct_callsite_information()
        {
            // Step 1. Create configuration object
            var config = new LoggingConfiguration();

            // Step 2. Create targets and add them to the configuration
            var target  = new MemoryTarget();
            var wrapper = new NLog.Targets.Wrappers.AsyncTargetWrapper(target)
            {
                TimeToSleepBetweenBatches = 0
            };

            config.AddTarget("target", wrapper);

            // Step 3. Set target properties
            target.Layout = "${date:format=HH\\:MM\\:ss} ${logger} ${message}";

            // Step 4. Define rules
            var rule = new LoggingRule("*", LogLevel.Debug, wrapper);

            config.LoggingRules.Add(rule);

            LogManager.Configuration = config;
            var factory = new NLogFactory(config);
            var logger  = factory.Create("MyLoggerName");

            WriteLogMessage(logger);
            LogManager.Flush();
            var logMessage = target.Logs[0];

            Assert.Contains("MyLoggerName", logMessage);

            // See that LogManager.ReconfigExistingLoggers() is able to upgrade the Logger
            target.Layout = "${date:format=HH\\:MM\\:ss} ${logger} ${callsite} ${message}";
            LogManager.ReconfigExistingLoggers();
            WriteLogMessage(logger);
            LogManager.Flush();
            logMessage = target.Logs[1];
            Assert.Contains("CallSiteTests.WriteLogMessage", logMessage);

            // See that LogManager.ReconfigExistingLoggers() is able to upgrade the Logger
            target.Layout = "${date:format=HH\\:MM\\:ss} ${logger} ${callsite} ${message} ThreadId=${threadid}";
            LogManager.ReconfigExistingLoggers();
            WriteLogMessage(logger);
            LogManager.Flush();
            logMessage = target.Logs[2];
            Assert.Contains("ThreadId=" + Thread.CurrentThread.ManagedThreadId.ToString(), logMessage);

            // See that interface logging also works (Improve support for Microsoft.Extension.Logging.ILogger replacement)
            INLogLogger ilogger = logger;

            WriteLogMessage(ilogger);
            LogManager.Flush();
            logMessage = target.Logs[3];
            Assert.Contains("CallSiteTests.WriteLogMessage", logMessage);
        }
Exemplo n.º 4
0
        private void WriteLogMessage(NLogFactory factory)
        {
            var logger = factory.Create("MyLoggerName");

            logger.Debug("something");
        }
Exemplo n.º 5
0
 private void WriteLogMessage(NLogFactory factory)
 {
     var logger = factory.Create("MyLoggerName");
     logger.Debug("something");
 }