private void ToLogstashLevelTests(LogLevel logLevel, LogstashLevel expectedLevel)
        {
            var converter = new LogLevelConverter();
            var level     = converter.ToLogStashLevel(logLevel);

            Assert.Equal(expectedLevel, level);
        }
Exemple #2
0
        public LogMessage Build(string loggerName, LogLevel logLevel, object state, Exception exception, Func <object, Exception, string> formatter = null)
        {
            if (state == null && exception == null)
            {
                return(null);
            }



            var logstashLevel = LogLevelConverter.ToLogStashLevel(logLevel);
            var logMessage    = new LogMessage(logstashLevel);

            string message;

            if (formatter != null)
            {
                message = formatter(state, exception);
            }
            else
            {
                message = Microsoft.Extensions.Logging.LogFormatter.Formatter(state, exception);
            }

            if (!string.IsNullOrEmpty(message))
            {
                logMessage.Header.Correlation   = BuildCorrelation();
                logMessage.Header.Index         = Options.Index;
                logMessage.Header.Source        = new LogMessageSource(Options.AppId, loggerName);
                logMessage.Header.TimeStamp     = DateTime.Now;
                logMessage.Header.VersionNumber = Defaults.Message.HeaderVersion;
                logMessage.Header.IPAddress     = LocalIPAddress;
                logMessage.Header.ProcessId     = CurrentProcess;
                logMessage.Header.ThreadId      = Thread.CurrentThread.ManagedThreadId.ToString();

                //logMessage.Body.User = new LogMessageUser() { UserId = Thread.CurrentPrincipal?.Identity?.Name, IPAddress = LocalIPAddress };       // ToDo (SVB) : where does user's ip address come from?
                logMessage.Body.User = new LogMessageUser()
                {
                    UserId = "ss", IPAddress = LocalIPAddress
                };
                logMessage.Body.VersionNumber = Options.MessageVersion;
                logMessage.Body.Content       = message;
                //logMessage.Body.Content = Serialize(state);     // ??
            }

            return(logMessage);
        }