Exemplo n.º 1
0
//        public static LoggerFactory UseConfiguration(this LoggerFactory loggerFactory, Stream jsonStream)
//        {
//            return loggerFactory.UseConfiguration(LoggerFactoryConfiguration.Load(jsonStream));
//        }

        public static LoggerFactory UseConverter <T>(this LoggerFactory loggerFactory, WriteJsonCallback <T> convert)
        {
            var converter = new SimpleJsonConverter <T>(convert);

            return(loggerFactory);
        }
Exemplo n.º 2
0
        public static void SemanticExtensions()
        {
            SmartPropertiesLayoutRenderer.Register();

            //var fileProvider = new RelativeResourceProvider(new PhysicalFileProvider(), typeof(Demo).Assembly.Location);

            var loggerFactory =
                new LoggerFactory()
                .AttachObject("Environment", "Demo")
                .AttachObject("Product", "Reusable.Apps.Console")
                .AttachScope()
                .AttachSnapshot(SimpleJsonConverter <Person> .Create(x => new { x.FirstName, x.LastName }))
                .Attach <Timestamp <DateTimeUtc> >()
                .AttachElapsedMilliseconds()
                .AddObserver <NLogRx>();
            // UseConverter<Something>(x => x.ToString());
            //.UseConfiguration(LoggerFactoryConfiguration.Load(fileProvider.GetFileInfo(@"cfg\omnilog.json").CreateReadStream()));

            var logger = loggerFactory.CreateLogger("Demo");

            logger.Log(Abstraction.Layer.Infrastructure().Routine("SemLogTest").Running());
            logger.Log(Abstraction.Layer.Infrastructure().Meta(new { Null = (string)null }));


            // Opening outer-transaction.
            using (logger.BeginScope().WithCorrelationHandle("Blub").WithRoutine("MyRoutine").WithCorrelationContext(new { Name = "OuterScope", CustomerId = 123 }).AttachElapsed())
            {
                // Logging some single business variable and a message.
                logger.Log(Abstraction.Layer.Business().Variable(new { foo = "bar" }), log => log.Message("Hallo variable!"));
                logger.Log(Abstraction.Layer.Database().Counter(new { RowCount = 7 }));
                logger.Log(Abstraction.Layer.Database().Decision("blub").Because("bluby"));

                // Opening inner-transaction.
                using (logger.BeginScope().WithCorrelationContext(new { Name = "InnerScope", ItemId = 456 }).AttachElapsed())
                {
                    logger.Log(Abstraction.Layer.Infrastructure().RoutineFromScope().Running());

                    var correlationIds = logger.Scopes().CorrelationIds <string>().ToList();

                    // Logging an entire object in a single line.
                    //var customer = new { FirstName = "John", LastName = "Doe" };
                    var customer = new Person
                    {
                        FirstName       = "John",
                        LastName        = null,
                        Age             = 123.456,
                        DBNullTest      = DBNull.Value,
                        GraduationYears = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 },
                        Nicknames       = { "Johny", "Doe" }
                    };
                    logger.Log(Abstraction.Layer.Business().Variable(new { customer }));

                    // Logging multiple variables in a single line.
                    var baz = 123;
                    var qux = "quux";

                    logger.Log(Abstraction.Layer.Infrastructure().Composite(new { multiple = new { baz, qux } }));

                    // Logging action results.
                    logger.Log(Abstraction.Layer.Infrastructure().Routine("DoSomething").Running());
                    logger.Log(Abstraction.Layer.Infrastructure().Routine("DoSomething").Canceled().Because("No connection."));
                    logger.Log(Abstraction.Layer.Infrastructure().Routine("DoSomething").Faulted(), new DivideByZeroException("Cannot divide."));
                    logger.Log(Abstraction.Layer.Service().Decision("Don't do this.").Because("Disabled."));
                }
            }
        }