Пример #1
0
 static void ConfigureCommonLogging()
 {
     var nameValueCollection = new NameValueCollection();
     var nlogAdapter = new NLogLoggerFactoryAdapter(nameValueCollection);
     Common.Logging.LogManager.Adapter = nlogAdapter;
     LoggingPcl::Common.Logging.LogManager.Adapter = nlogAdapter;
 }
Пример #2
0
        public static IServiceCollection AddCommonLogging(this IServiceCollection services)
        {
            var properties  = new NameValueCollection();
            var nlogAdapter = new NLogLoggerFactoryAdapter(properties);

            LogManager.Adapter = nlogAdapter;

            var config = new NLog.Config.LoggingConfiguration();

            config.AddTarget("console", new ConsoleTarget
            {
                Layout = "${my-layout}"
            });
            config.AddRule(NLog.LogLevel.Trace, NLog.LogLevel.Fatal, "console");
            NLog.Config.ConfigurationItemFactory.Default.RegisterItemsFromAssembly(Assembly.GetExecutingAssembly());

            NLog.LogManager.Configuration = config;

            return(services.AddScoped <ILog>(sp =>
            {
                var context = sp.GetRequiredService <DefaultServiceContext>();

                // inject context info to Logger
                NLog.MappedDiagnosticsLogicalContext.Set("corp-id", context.CorpId);
                NLog.MappedDiagnosticsLogicalContext.Set("user-id", context.UserId);

                return LogManager.GetLogger("app");
            }));
        }
Пример #3
0
        public void CheckNestedThreadVariablesSet()
        {
            var a = new NLogLoggerFactoryAdapter((NameValueCollection)null);

            var hasItems = a.GetLogger(this.GetType()).NestedThreadVariablesContext.HasItems;

            Assert.AreEqual(false, hasItems);

            a.GetLogger(this.GetType()).NestedThreadVariablesContext.Push("TestValue1");

            hasItems = a.GetLogger(this.GetType()).NestedThreadVariablesContext.HasItems;
            Assert.AreEqual(true, hasItems);

            a.GetLogger(this.GetType()).NestedThreadVariablesContext.Push("TestValue2");

            int depth = global::NLog.NestedDiagnosticsContext.GetAllMessages().Length;

            Assert.AreEqual(2, depth);

            var actualValue = a.GetLogger(this.GetType()).NestedThreadVariablesContext.Pop();

            Assert.AreEqual("TestValue2", actualValue);

            actualValue = a.GetLogger(this.GetType()).NestedThreadVariablesContext.Pop();
            Assert.AreEqual("TestValue1", actualValue);

            hasItems = a.GetLogger(this.GetType()).NestedThreadVariablesContext.HasItems;
            Assert.AreEqual(false, hasItems);

            depth = global::NLog.NestedDiagnosticsContext.GetAllMessages().Length;
            Assert.AreEqual(0, depth);
        }
Пример #4
0
        public void CheckThreadVariablesSet()
        {
            var a = new NLogLoggerFactoryAdapter((Common.Logging.Configuration.NameValueCollection)null);

            a.GetLogger(this.GetType()).ThreadVariablesContext.Set("TestKey", "TestValue");

            // NLog10 doesn't support this. This test only makes sure that no exceptions are thrown
        }
Пример #5
0
        static void ConfigureCommonLogging()
        {
            var nameValueCollection = new NameValueCollection();
            var nlogAdapter         = new NLogLoggerFactoryAdapter(nameValueCollection);

            Common.Logging.LogManager.Adapter             = nlogAdapter;
            LoggingPcl::Common.Logging.LogManager.Adapter = nlogAdapter;
        }
Пример #6
0
        public void CheckThreadVariablesSet()
        {
            var a = new NLogLoggerFactoryAdapter((Common.Logging.Configuration.NameValueCollection)null);

            a.GetLogger(this.GetType()).ThreadVariablesContext.Set("TestKey", "TestValue");

            var actualValue = global::NLog.MappedDiagnosticsContext.Get("TestKey");

            Assert.AreEqual("TestValue", actualValue);
        }
Пример #7
0
        public void CheckGlobalVariablesSet()
        {
            var a         = new NLogLoggerFactoryAdapter((Common.Logging.Configuration.NameValueCollection)null);
            var testValue = new object();

            a.GetLogger(this.GetType()).GlobalVariablesContext.Set("TestKey", testValue);

            var actualValue = global::NLog.GlobalDiagnosticsContext.GetObject("TestKey");

            Assert.AreEqual(testValue, actualValue);
        }
Пример #8
0
        public void TestSetUp()
        {
            _log4NetLoggerFactoryAdapter  = new Log4NetLoggerFactoryAdapter(new NameValueCollection());
            _nLogLoggerFactoryAdapter     = new NLogLoggerFactoryAdapter(new NameValueCollection());
            _multipleLoggerFactoryAdapter = new MultiLoggerFactoryAdapter();

            _multipleLoggerFactoryAdapter.LoggerFactoryAdapters.Add(_log4NetLoggerFactoryAdapter);
            _multipleLoggerFactoryAdapter.LoggerFactoryAdapters.Add(_nLogLoggerFactoryAdapter);

            //these tests will only work if all of the loggers actually *support* VariablesContext with other than the No-Op 'placeholder'
            Assume.That(_log4NetLoggerFactoryAdapter.GetLogger(typeof(MultipleLoggerGlobalVariableContextTests)).GlobalVariablesContext, Is.Not.InstanceOf <NoOpVariablesContext>());
            Assume.That(_nLogLoggerFactoryAdapter.GetLogger(typeof(MultipleLoggerGlobalVariableContextTests)).GlobalVariablesContext, Is.Not.InstanceOf <NoOpVariablesContext>());
        }