Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1014,7 +1014,7 @@ public static void WriteEntry(string source, string? message, EventLogEntryType

public static void WriteEntry(string source, string? message, EventLogEntryType type, int eventID, short category, byte[]? rawData)
{
using (EventLogInternal log = new EventLogInternal(string.Empty, ".", CheckAndNormalizeSourceName(source), parent: null! /* Special case - EventLogInternal instance is immediately used */))
using (EventLogInternal log = new EventLogInternal(string.Empty, ".", CheckAndNormalizeSourceName(source), parent: null))
{
log.WriteEntry(message, type, eventID, category, rawData);
}
Expand All @@ -1037,15 +1037,15 @@ public void WriteEvent(EventInstance instance, byte[]? data, params object?[]? v

public static void WriteEvent(string source, EventInstance instance, params object?[]? values)
{
using (EventLogInternal log = new EventLogInternal(string.Empty, ".", CheckAndNormalizeSourceName(source), parent: null! /* Special case - EventLogInternal instance is immediately used */))
using (EventLogInternal log = new EventLogInternal(string.Empty, ".", CheckAndNormalizeSourceName(source), parent: null))
{
log.WriteEvent(instance, null, values);
}
}

public static void WriteEvent(string source, EventInstance instance, byte[] data, params object?[]? values)
{
using (EventLogInternal log = new EventLogInternal(string.Empty, ".", CheckAndNormalizeSourceName(source), parent: null! /* Special case - EventLogInternal instance is immediately used */))
using (EventLogInternal log = new EventLogInternal(string.Empty, ".", CheckAndNormalizeSourceName(source), parent: null))
{
log.WriteEvent(instance, data, values);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ public bool EnableRaisingEvents
{
string currentMachineName = this.machineName;

if (parent!.ComponentDesignMode)
if (parent?.ComponentDesignMode == true)
this.boolFlags[Flag_monitoring] = value;
else
{
Expand Down Expand Up @@ -377,7 +377,7 @@ public ISynchronizeInvoke? SynchronizingObject
{
get
{
if (this.synchronizingObject == null && parent!.ComponentDesignMode)
if (this.synchronizingObject == null && parent?.ComponentDesignMode == true)
{
IDesignerHost? host = (IDesignerHost?)parent.ComponentGetService(typeof(IDesignerHost));
if (host != null)
Expand Down Expand Up @@ -559,15 +559,17 @@ private void CompletionCallback()
while (i < count)
{
EventLogEntry entry = GetEntryWithOldest(i);
if (onEntryWrittenHandler != null)
EntryWrittenEventHandler? handler = onEntryWrittenHandler;
if (handler is not null)
{
if (this.SynchronizingObject != null && this.SynchronizingObject.InvokeRequired)
ISynchronizeInvoke? synchronizingObject = SynchronizingObject;
if (synchronizingObject is not null && synchronizingObject.InvokeRequired)
{
this.SynchronizingObject.BeginInvoke(this.onEntryWrittenHandler, new object[] { this, new EntryWrittenEventArgs(entry) });
synchronizingObject.BeginInvoke(handler, new object[] { this, new EntryWrittenEventArgs(entry) });
}
else
{
onEntryWrittenHandler(this, new EntryWrittenEventArgs(entry));
handler(this, new EntryWrittenEventArgs(entry));
}
}

Expand Down Expand Up @@ -1173,7 +1175,7 @@ private void StartListening(string currentMachineName, string currentLogName)

private void StartRaisingEvents(string currentMachineName, string currentLogName)
{
if (!boolFlags[Flag_initializing] && !boolFlags[Flag_monitoring] && !parent!.ComponentDesignMode)
if (!boolFlags[Flag_initializing] && !boolFlags[Flag_monitoring] && parent?.ComponentDesignMode != true)
Comment thread
tarekgh marked this conversation as resolved.
{
StartListening(currentMachineName, currentLogName);
}
Expand Down Expand Up @@ -1216,7 +1218,7 @@ private void StopListening(/*string currentMachineName,*/ string currentLogName)

private void StopRaisingEvents(/*string currentMachineName,*/ string currentLogName)
{
if (!boolFlags[Flag_initializing] && boolFlags[Flag_monitoring] && !parent!.ComponentDesignMode)
if (!boolFlags[Flag_initializing] && boolFlags[Flag_monitoring] && parent?.ComponentDesignMode != true)
Comment thread
tarekgh marked this conversation as resolved.
{
StopListening(currentLogName);
}
Expand Down Expand Up @@ -1360,11 +1362,11 @@ private void InternalWriteEvent(uint eventID, ushort category, EventLogEntryType

for (int i = 0; i < strings.Length; i++)
{
strings[i] ??= string.Empty;
string s = strings[i] ??= string.Empty;

// make sure the strings aren't too long. MSDN says each string has a limit of 32k (32768) characters, but
// experimentation shows that it doesn't like anything larger than 32766
if (strings[i]!.Length > 32766)
if (s.Length > 32766)
throw new ArgumentException(SR.LogEntryTooLong);
}
rawData ??= Array.Empty<byte>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,15 @@ public override void TraceEvent(TraceEventCache? eventCache, string source, Trac
if (Filter != null && !Filter.ShouldTrace(eventCache, source, severity, id, format, args, null, null))
return;

EventLog? eventLog = EventLog;
if (eventLog is null)
return;

EventInstance data = CreateEventInstance(severity, id);

if (args == null || args.Length == 0)
{
EventLog!.WriteEvent(data, format);
eventLog.WriteEvent(data, format);
}
else if (string.IsNullOrEmpty(format))
{
Expand All @@ -99,13 +103,12 @@ public override void TraceEvent(TraceEventCache? eventCache, string source, Trac
strings[i] = args[i]?.ToString();
}

EventLog!.WriteEvent(data, strings);
eventLog.WriteEvent(data, strings);
}
else
{
EventLog!.WriteEvent(data, string.Format(CultureInfo.InvariantCulture, format, args));
eventLog.WriteEvent(data, string.Format(CultureInfo.InvariantCulture, format, args));
}

}

[ComVisible(false)]
Expand All @@ -114,7 +117,11 @@ public override void TraceEvent(TraceEventCache? eventCache, string source, Trac
if (Filter != null && !Filter.ShouldTrace(eventCache, source, severity, id, message, null, null, null))
return;

EventLog!.WriteEvent(CreateEventInstance(severity, id), message);
EventLog? eventLog = EventLog;
if (eventLog is null)
return;

eventLog.WriteEvent(CreateEventInstance(severity, id), message);
}

[ComVisible(false)]
Expand All @@ -123,7 +130,11 @@ public override void TraceData(TraceEventCache? eventCache, string source, Trace
if (Filter != null && !Filter.ShouldTrace(eventCache, source, severity, id, null, null, data, null))
return;

EventLog!.WriteEvent(CreateEventInstance(severity, id), new object?[] { data });
EventLog? eventLog = EventLog;
if (eventLog is null)
return;

eventLog.WriteEvent(CreateEventInstance(severity, id), new object?[] { data });
}

[ComVisible(false)]
Expand All @@ -132,6 +143,10 @@ public override void TraceData(TraceEventCache? eventCache, string source, Trace
if (Filter != null && !Filter.ShouldTrace(eventCache, source, severity, id, null, null, null, data))
return;

EventLog? eventLog = EventLog;
if (eventLog is null)
return;

EventInstance inst = CreateEventInstance(severity, id);

var sb = new StringBuilder();
Expand All @@ -148,7 +163,7 @@ public override void TraceData(TraceEventCache? eventCache, string source, Trace
}
}

EventLog!.WriteEvent(inst, new object[] { sb.ToString() });
eventLog.WriteEvent(inst, new object[] { sb.ToString() });
}

private static EventInstance CreateEventInstance(TraceEventType severity, int id)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,17 @@ public void EventLogConstructor(EventLog eventLog, string expected)
}
}

[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotNetFramework))]
public void TraceMethods_EventLogNull_NoOp()
{
using var listener = new EventLogTraceListener(eventLog: null);

listener.TraceEvent(null, "source", TraceEventType.Information, 1, "message");
listener.TraceEvent(null, "source", TraceEventType.Information, 1, "{0}", "message");
listener.TraceData(null, "source", TraceEventType.Information, 1, data: (object?)"message");
listener.TraceData(null, "source", TraceEventType.Information, 1, data: new object?[] { "message" });
}

[Fact]
public void StringConstructor()
{
Expand Down
Loading