Пример #1
0
        private static Action <DateTime, int, string, Exception> SerilogLogHandlerProvider(string loggerName)
        {
            var seriLogLogger = new LoggerConfiguration().MinimumLevel.Verbose().WriteTo.Console().WriteTo.File("serilog\\serilog.log").CreateLogger();

            return(new Action <DateTime, int, string, Exception>(delegate(DateTime timeStamp, int logLevel, string message, Exception exception)
            {
                DateTimeOffset timeStampOffset = new DateTimeOffset(timeStamp);

                List <Serilog.Parsing.MessageTemplateToken> tokens = new List <Serilog.Parsing.MessageTemplateToken>();
                Serilog.Parsing.MessageTemplateParser parser = new Serilog.Parsing.MessageTemplateParser();

                Serilog.Events.MessageTemplate messageTemplate = _seriLogMessageTemplateParser.Parse(message);

                List <Serilog.Events.LogEventProperty> properties = new List <Serilog.Events.LogEventProperty>();

                Serilog.Events.LogEvent logEvent = new Serilog.Events.LogEvent(timeStampOffset, _seriLogLogLevelArray[logLevel], exception, messageTemplate, properties);

                seriLogLogger.Write(logEvent);
            }));
        }
Пример #2
0
        protected override void Write(LogEventInfo logEvent)
        {
            var log           = Log.ForContext(Serilog.Core.Constants.SourceContextPropertyName, logEvent.LoggerName);
            var logEventLevel = ConvertLevel(logEvent.Level);

            if ((logEvent.Parameters?.Length ?? 0) == 0)
            {
                // NLog treats a single string as a verbatim string; Serilog treats it as a String.Format format and hence collapses doubled braces
                // This is the most direct way to emit this without it being re-processed by Serilog (via @nblumhardt)
                var template = new Serilog.Events.MessageTemplate(new[] { new Serilog.Parsing.TextToken(logEvent.FormattedMessage) });
                log.Write(new Serilog.Events.LogEvent(DateTimeOffset.Now, logEventLevel, logEvent.Exception, template, Enumerable.Empty <Serilog.Events.LogEventProperty>()));
            }
            else
            // Risk: tunneling an NLog format and assuming it will Just Work as a Serilog format
#pragma warning disable Serilog004 // Constant MessageTemplate verifier
            {
                log.Write(logEventLevel, logEvent.Exception, logEvent.Message, logEvent.Parameters);
            }
#pragma warning restore Serilog004
        }