Ejemplo n.º 1
0
        public void Logging()
        {
            const string message       = "Error Message";
            const string messageFormat = "Message Format: message: {0}, exception: {1}";
            var          ex            = new Exception();

            var log = new SerilogLogger(GetType());

            Assert.IsNotNull(log);

            log.Debug(message);
            log.Debug(message, ex);
            log.DebugFormat(messageFormat, message, ex.Message);
            log.Debug(ex, messageFormat, messageFormat, ex);

            log.Error(message);
            log.Error(message, ex);
            log.ErrorFormat(messageFormat, message, ex.Message);
            log.Error(ex, messageFormat, messageFormat, ex);

            log.Fatal(message);
            log.Fatal(message, ex);
            log.FatalFormat(messageFormat, message, ex.Message);
            log.Fatal(ex, messageFormat, messageFormat, ex);

            log.Info(message);
            log.Info(message, ex);
            log.InfoFormat(messageFormat, message, ex.Message);
            log.Info(ex, messageFormat, messageFormat, ex);

            log.Warn(message);
            log.Warn(message, ex);
            log.WarnFormat(messageFormat, message, ex.Message);
            log.Warn(ex, messageFormat, messageFormat, ex);
        }
Ejemplo n.º 2
0
        public void Fatal_LambdaMessage()
        {
            // Prepare
            const string message = "Simple Message";

            logger.Setup(l => l.Write(
                             LogEventLevel.Fatal, message, null))
            .Verifiable();
            log = new SerilogLogger(logger.Object);

            // Act
            log.Fatal(message);

            // Assert
            logger.VerifyAll();
        }
Ejemplo n.º 3
0
        public void Fatal_MessageWithException()
        {
            // Prepare
            const string message = "Exception occurred";
            var          ex      = new Exception(message);

            logger.Setup(l => l.Write(
                             LogEventLevel.Fatal, ex, message, null))
            .Verifiable();
            log = new SerilogLogger(logger.Object);

            // Act
            log.Fatal(message, ex);

            // Assert
            logger.VerifyAll();
        }
Ejemplo n.º 4
0
        static void Main(string[] args)
        {
            // Log configuration should be setup on client app side, so I left this here.
            // I don't like an idea to add Serilog packages to this project, but I see no other way at the moment.
            // Every so called sink (ColoredConsole, File, etc) is a standalone package, so configuration must be done here.
            // There is another aproach - a congig file. If we go with config file, then probably we can avoid issue above.

            /* we cannot configure Serilog inside client app cuz we are going to hide log provider from end users behind our wrapper
             * var cfg = new LoggerConfiguration()
             *  .Enrich.FromLogContext()
             *  .MinimumLevel.Debug()
             *  .WriteTo.ColoredConsole(
             *      LogEventLevel.Verbose,
             *      "{NewLine}{Timestamp:HH:mm:ss} [{Level}] ({CorrelationToken}) {Message}{NewLine}{Exception}");
             */

            var configuration = new ConfigurationBuilder()
                                .AddJsonFile("appsettings.json")
                                .Build();

            using (ILogger logger = new SerilogLogger(configuration))
            {
                //Simple log entry
                logger.Debug("Hello World!");

                //Parameterized log entry
                var quota = 100;
                var user  = new { FirstName = "John", LastName = "Walter" };
                logger.Write(LogLevel.Warning, "Disk quota {Quota} MB exceeded by {User}", quota, user);

                //Exception handling
                var myDataObject = new { Property1 = "Value1", Property2 = "Value2", Property3 = "Value3" };
                try
                {
                    int x = 0;
                    int y = 10 / x;
                }
                catch (Exception ex)
                {
                    logger.Fatal("Got an exception for {MyDataObject}", ex, myDataObject);
                }
            }

            Console.ReadKey();
        }