From cdcd3cb50a1da512fb00b47ebe3ec10dc3689502 Mon Sep 17 00:00:00 2001 From: Matthew Collera Date: Fri, 28 May 2021 14:08:15 -0400 Subject: [PATCH 1/2] Added properties to eventdata --- .../Sinks/EventLog/EventLogSink.cs | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/Serilog.Sinks.EventLog/Sinks/EventLog/EventLogSink.cs b/src/Serilog.Sinks.EventLog/Sinks/EventLog/EventLogSink.cs index efaa4c7..353b7a3 100644 --- a/src/Serilog.Sinks.EventLog/Sinks/EventLog/EventLogSink.cs +++ b/src/Serilog.Sinks.EventLog/Sinks/EventLog/EventLogSink.cs @@ -12,13 +12,15 @@ // See the License for the specific language governing permissions and // limitations under the License. -using System; -using System.Diagnostics; -using System.IO; using Serilog.Core; using Serilog.Debugging; using Serilog.Events; using Serilog.Formatting; +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.IO; +using System.Linq; namespace Serilog.Sinks.EventLog { @@ -158,7 +160,14 @@ public void Emit(LogEvent logEvent) payload = payload.Substring(0, MaximumPayloadLengthChars); } - _log.WriteEntry(payload, type, _eventIdProvider.ComputeEventId(logEvent)); + var eventInstance = new EventInstance(_eventIdProvider.ComputeEventId(logEvent), 0, type); + var parameters = new List() + { + payload, + }; + parameters.AddRange(logEvent.Properties.Values.Select(x => x.ToString())); + + _log.WriteEvent(eventInstance, parameters.ToArray()); } static EventLogEntryType LevelToEventLogEntryType(LogEventLevel logEventLevel) From 50781788e9a626ed803feaf85005e60a12376809 Mon Sep 17 00:00:00 2001 From: Matthew Collera Date: Fri, 28 May 2021 16:30:10 -0400 Subject: [PATCH 2/2] Fixed payload size --- .../Sinks/EventLog/EventLogSink.cs | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/Serilog.Sinks.EventLog/Sinks/EventLog/EventLogSink.cs b/src/Serilog.Sinks.EventLog/Sinks/EventLog/EventLogSink.cs index 353b7a3..ed46e09 100644 --- a/src/Serilog.Sinks.EventLog/Sinks/EventLog/EventLogSink.cs +++ b/src/Serilog.Sinks.EventLog/Sinks/EventLog/EventLogSink.cs @@ -31,7 +31,7 @@ namespace Serilog.Sinks.EventLog public class EventLogSink : ILogEventSink { const string ApplicationLogName = "Application"; - const int MaximumPayloadLengthChars = 31839; + const int MaximumPayloadLengthChars = 31639; const int MaximumSourceNameLengthChars = 212; const int SourceMovedEventId = 3; @@ -160,12 +160,25 @@ public void Emit(LogEvent logEvent) payload = payload.Substring(0, MaximumPayloadLengthChars); } + var payloadSize = payload.Length; var eventInstance = new EventInstance(_eventIdProvider.ComputeEventId(logEvent), 0, type); var parameters = new List() { payload, }; - parameters.AddRange(logEvent.Properties.Values.Select(x => x.ToString())); + + foreach (var property in logEvent.Properties) + { + if (payloadSize >= MaximumPayloadLengthChars) + { + break; + } + + var value = property.Value.ToString(); + var valueLength = Math.Min(value.Length, MaximumPayloadLengthChars - payloadSize); + parameters.Add(value.Substring(0, valueLength)); + payloadSize += valueLength; + } _log.WriteEvent(eventInstance, parameters.ToArray()); }