Пример #1
0
        /// <summary>
        /// Writes the supplied <see cref="LogEvent"/> to <see cref="System.Diagnostics.Trace"/>.
        /// </summary>
        /// <remarks>
        /// <para>The <see cref="LogEvent.EventSeverity"/> is used to decide which <see cref="System.Diagnostics.Trace"/> method is called to log the event.</para>
        /// </remarks>
        /// <param name="logEvent">A <see cref="LogEvent"/> instance to write.</param>
        protected override void WriteEventInternal(LogEvent logEvent)
        {
            if (logEvent == null)
            {
                throw new ArgumentNullException(nameof(logEvent));
            }

            var formattedMessage = _LogEventFormatter.FormatToString(logEvent) ?? String.Empty;

            if (formattedMessage.Contains("{") || formattedMessage.Contains("}"))
            {
                formattedMessage = formattedMessage.Replace("{", "{{").Replace("}", "}}");
            }

            switch (logEvent.EventSeverity)
            {
            case LogEventSeverity.Information:
                System.Diagnostics.Trace.TraceInformation(formattedMessage);
                break;

            case LogEventSeverity.Error:
                System.Diagnostics.Trace.TraceError(formattedMessage);
                break;

            case LogEventSeverity.Warning:
                System.Diagnostics.Trace.TraceWarning(formattedMessage);
                break;

            default:
                System.Diagnostics.Trace.Write(formattedMessage, logEvent.EventSeverity.ToString());
                break;
            }
        }
Пример #2
0
            private object GetValueByName(string name)
            {
                switch (name)
                {
                case nameof(LogEvent.DateTime):
                    return(CurrentEvent.DateTime.DateTime);

                case nameof(LogEvent.EventName):
                    return(Truncate(nameof(LogEvent.EventName), CurrentEvent.EventName ?? String.Empty));

                case nameof(LogEvent.EventSeverity):
                    return(CurrentEvent.EventSeverity);

                case nameof(LogEvent.EventType):
                    return(CurrentEvent.EventType);

                case nameof(LogEvent.Exception):
                    return(CurrentEvent.Exception);

                case nameof(LogEvent.Source):
                    return(Truncate(nameof(LogEvent.Source), CurrentEvent.Source ?? String.Empty));

                case nameof(LogEvent.SourceLineNumber):
                    return(CurrentEvent.SourceLineNumber);

                case nameof(LogEvent.SourceMethod):
                    return(Truncate(nameof(LogEvent.SourceMethod), CurrentEvent.SourceMethod));

                case SourceColumn_SerialisedLogEvent:
                    return(Truncate(SourceColumn_SerialisedLogEvent, _LogEventFormatter.FormatToString(CurrentEvent)));

                case SourceColumn_LogSeverityLevel:
                    return(Convert.ToInt32(CurrentEvent.EventSeverity, System.Globalization.CultureInfo.InvariantCulture));

                default:
                    var properties = CurrentEvent.Properties;
                    if (properties.ContainsKey(name))
                    {
                        var    retVal    = properties[name] ?? String.Empty;
                        string retValStr = retVal as string;
                        if (retVal is string)
                        {
                            return(Truncate(name, retValStr));
                        }
                        else
                        {
                            return(retVal);
                        }
                    }
                    else
                    {
                        return(null);
                    }
                }
            }
Пример #3
0
        protected override void WriteEventInternal(LogEvent logEvent)
        {
            if (logEvent == null)
            {
                return;
            }

            var label = (logEvent.EventName ?? $"{logEvent.EventSeverity.ToString()} {logEvent.EventType.ToString()} Log Event");

            label = label.Substring(Math.Min(124, label.Length));
            if (_Formatter == null)
            {
                _Queue.Send(logEvent, label);
            }
            else
            {
                _Queue.Send(_Formatter.FormatToString(logEvent), label);
            }
        }
Пример #4
0
        protected override void WriteEventInternal(LogEvent logEvent)
        {
            if (logEvent == null)
            {
                throw new ArgumentNullException(nameof(logEvent));
            }

            string message = null;

            if (_EventFormatter != null)
            {
                message = _EventFormatter.FormatToString(logEvent);
            }
            else
            {
                message = logEvent.EventName;

                if (logEvent.Exception != null)
                {
                    message = message += Environment.NewLine + logEvent.Exception.ToString();
                }
            }

            if (message != null && message.Length > 32000)
            {
                int startIndex = 0;
                int partCnt    = 1;
                while (startIndex < message.Length)
                {
                    int partSize = Math.Min(30000, message.Length - startIndex);
                    _Log.WriteEntry(String.Format(System.Globalization.CultureInfo.InvariantCulture, Properties.Resources.SplitEventEntryPrefix, partCnt.ToString(System.Globalization.CultureInfo.InvariantCulture)) + Environment.NewLine + message.Substring(startIndex, partSize), LogEventToWindowsEventType(logEvent), logEvent.GetPropertyOrDefault("EventId", 0), logEvent.GetPropertyOrDefault <short>("CategoryId", LogEventToWindowsEventCategory(logEvent)), logEvent.GetPropertyOrDefault <byte[]>("RawData", null));
                    startIndex += partSize;
                    partCnt++;
                }
            }
            else
            {
                _Log.WriteEntry(message, LogEventToWindowsEventType(logEvent), logEvent.GetPropertyOrDefault("EventId", 0), logEvent.GetPropertyOrDefault <short>("CategoryId", LogEventToWindowsEventCategory(logEvent)), logEvent.GetPropertyOrDefault <byte[]>("RawData", null));
            }
        }
Пример #5
0
        /// <summary>
        /// Writes the event to the console.
        /// </summary>
        /// <param name="logEvent">A <see cref="LogEvent"/> instance to write.</param>
        protected override void WriteEventInternal(LogEvent logEvent)
        {
            if (logEvent == null)
            {
                throw new ArgumentNullException(nameof(logEvent));
            }

            System.Diagnostics.Debugger.Log(Convert.ToInt32(logEvent.EventSeverity, System.Globalization.CultureInfo.InvariantCulture), logEvent.EventType.ToString(), _LogEventFormatter.FormatToString(logEvent));
        }