Example #1
0
        public void Execute(IEnumerable <LogMessage> messages)
        {
            foreach (var message in messages)
            {
                if (message.Message != null)
                {
                    continue;
                }

                var sb = new System.Text.StringBuilder(50);

                var dateTime = message.DateTime;

                sb.Append(dateTime.Year.ToString());
                sb.Append("-");
                sb.Append(FormatD2(dateTime.Month));
                sb.Append("-");
                sb.Append(FormatD2(dateTime.Day));
                sb.Append(" ");
                sb.Append(FormatD2(dateTime.Hour));
                sb.Append(":");
                sb.Append(FormatD2(dateTime.Minute));
                sb.Append(":");
                sb.Append(FormatD2(dateTime.Second));
                sb.Append(".");
                sb.Append(dateTime.Millisecond.ToString("D3"));

                sb.Append(_delimiter);

                switch (message.LogLevel)
                {
                case LogLevel.Trace:
                    sb.Append("TRACE");
                    break;

                case LogLevel.Debug:
                    sb.Append("DEBUG");
                    break;

                case LogLevel.Info:
                    sb.Append("INFO ");
                    break;

                case LogLevel.Warn:
                    sb.Append("WARN");
                    break;

                case LogLevel.Error:
                    sb.Append("ERROR");
                    break;

                case LogLevel.Fatal:
                    sb.Append("FATAL");
                    break;
                }

                sb.Append(_delimiter);
                sb.Append(message.Category);
                sb.Append(_delimiter);

                if (message.State != null)
                {
                    sb.Append(message.State.ToString());
                }

                sb.Append(_delimiter);

                var messageString = message.RawMessage;

                if (message.Arguments.Count > 0 && messageString.Length > 0)
                {
                    messageString = LogArgumentsParser.InsertArguments(messageString, message.Arguments);
                }

                sb.Append(messageString);

                if (message.Exception != null)
                {
                    sb.Append(message.Exception.ToString());
                }

                message.Message = sb.ToString();
            }

            NextMiddleware.Execute(messages);
        }
Example #2
0
        public void Execute(IEnumerable <LogMessage> messages)
        {
            if (messages == null)
            {
                return;
            }

            foreach (var message in messages)
            {
                var sb = new StringBuilder();

                sb.Append("{\"DateTime\":\"");
                sb.Append(message.DateTime.ToString("o", _enUsCultureInfo));
                sb.Append("\",\"LogLevel\":");
                sb.Append(((int)message.LogLevel).ToString(_enUsCultureInfo));
                sb.Append(",\"Category\":\"");
                AppendJson(sb, message.Category);
                sb.Append("\",\"State\":");

                if (message.State == null)
                {
                    sb.Append("null");
                }
                else
                {
                    sb.Append('\"');
                    AppendJson(sb, message.State.ToString());
                    sb.Append('\"');
                }

                sb.Append(",\"RawMessage\":\"");
                AppendJson(sb, message.RawMessage);
                sb.Append("\",\"Message\":\"");
                AppendJson(sb, LogArgumentsParser.InsertArguments(message.RawMessage, message.Arguments));
                sb.Append("\",\"Exception\":");

                if (message.Exception != null)
                {
                    sb.Append("\"");
                    AppendJson(sb, message.Exception.ToString());
                    sb.Append("\",\"ExceptionIsValid\":true");
                }
                else
                {
                    sb.Append("null,\"ExceptionIsValid\":false");
                }

                sb.Append(",\"Arguments\":[");

                var isFirst = true;

                foreach (var arg in message.Arguments)
                {
                    if (isFirst)
                    {
                        isFirst = false;
                    }
                    else
                    {
                        sb.Append(',');
                    }

                    SerializeArgument(sb, arg);
                }

                sb.Append("]}");

                message.Message = sb.ToString();
            }

            NextMiddleware.Execute(messages);
        }