string msg = owner.FormatMessageWrapper(dllNames, (uint)msgId, ReplacementStrings);
if (msg == null)
{
- StringBuilder msgBuf = new StringBuilder(SR.MessageNotFormatted + msgId + Source);
+ StringBuilder msgBuf = new StringBuilder(SR.Format(SR.MessageNotFormatted, msgId, Source));
string[] strings = ReplacementStrings;
for (int i = 0; i < strings.Length; i++)
{
{
public class EventInstanceTests
{
- //These ids can be any non-negative numbers
+ // These ids can be any non-negative numbers
private const long instanceId = 57;
private const int categoryId = 657;
[ConditionalFact(typeof(Helpers), nameof(Helpers.IsElevatedAndSupportsEventLogs))]
public void EntryWrittenEventRaiseDisable()
{
- eventCounter = 0;
RaisingEvent("DisableEvent", nameof(EntryWrittenEventRaiseDisable), waitOnEvent: false);
Assert.Equal(0, eventCounter);
}
{
EventLog.CreateEventSource(source, null);
Assert.True(EventLog.SourceExists(source));
- Assert.Equal(EventLog.LogNameFromSourceName(source, "."), "Application");
+ Assert.Equal("Application", EventLog.LogNameFromSourceName(source, "."));
}
finally
{
public void CategoryResourceFile_Set()
{
string messageFile = "ResourceFile";
- string source = "Source" + nameof(MessageResourceFile_Set);
+ string source = "Source" + nameof(CategoryResourceFile_Set);
string log = "MessageResourceFile";
EventSourceCreationData sourceData = new EventSourceCreationData(source, log);
sourceData.CategoryResourceFile = messageFile;
public void ParameterResourceFile_Set()
{
string messageFile = "ResourceFile";
- string source = "Source" + nameof(MessageResourceFile_Set);
+ string source = "Source" + nameof(ParameterResourceFile_Set);
string log = "MessageResourceFile";
EventSourceCreationData sourceData = new EventSourceCreationData(source, log);
sourceData.ParameterResourceFile = messageFile;
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotWindowsNanoServer))]
public void CategoryCount_Set()
{
- string source = "Source" + nameof(MessageResourceFile_Set);
+ string source = "Source" + nameof(CategoryCount_Set);
string log = "MessageResourceFile";
EventSourceCreationData sourceData = new EventSourceCreationData(source, log);
sourceData.CategoryCount = 2;
eventLog.Clear();
Assert.Equal(0, eventLog.Entries.Count);
eventLog.WriteEntry("Writing to event log.");
- Assert.Equal(eventLog.Entries.Count, 1);
+ Assert.Equal(1,eventLog.Entries.Count);
}
}
finally
EventLog.DeleteEventSource(source);
EventLog.Delete(log);
}
-
}
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotWindowsNanoServer))]
{
eventLog.Source = source;
- //The second argument is only used when the overflow policy is set to OverWrite Older
+ // The second argument is only used when the overflow policy is set to OverWrite Older
eventLog.ModifyOverflowPolicy(OverflowAction.DoNotOverwrite, 1);
Assert.Equal(OverflowAction.DoNotOverwrite, eventLog.OverflowAction);
- // -1 means overflow action is donot overrite
+ // -1 means overflow action is donot OverWrite
Assert.Equal(-1, eventLog.MinimumRetentionDays);
}
}
{
string source = "Source_" + nameof(OverflowAndRetention_Set);
string log = "Overflow_Set";
- int retentionDays = 30; //A number between 0 and 365 should work
+ int retentionDays = 30; // A number between 0 and 365 should work
try
{
{
eventLog.Source = source;
- //The second argument is only used when the overflow policy is set to OverWrite Older
+ // The second argument is only used when the overflow policy is set to OverWrite Older
eventLog.ModifyOverflowPolicy(OverflowAction.OverwriteOlder, retentionDays);
Assert.Equal(OverflowAction.OverwriteOlder, eventLog.OverflowAction);
Assert.Equal(retentionDays, eventLog.MinimumRetentionDays);
string log = "DisplayName";
string source = "Source_" + nameof(RegisterDisplayLogName);
string messageFile = GetTestFilePath();
- long DisplayNameMsgId = 45;
+ long DisplayNameMsgId = 42; // It could be any number
EventSourceCreationData sourceData = new EventSourceCreationData(source, log);
try
EventLog.DeleteEventSource(source);
EventLog.Delete(log);
}
-
}
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotWindowsNanoServer))]
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotWindowsNanoServer))]
public void EventLog_EnableRaisingEvents_DefaultFalse()
{
- Assert.False(new EventLog("log").EnableRaisingEvents);
+ using (EventLog eventLog = new EventLog("log"))
+ {
+ Assert.False(eventLog.EnableRaisingEvents);
+ }
}
[ConditionalFact(typeof(Helpers), nameof(Helpers.IsElevatedAndSupportsEventLogs))]
eventLog.Source = source;
return eventLog.Entries[eventLog.Entries.Count - 1];
}
-
}
private EventLogEntry WriteLogEntryEvent(string source, bool data = false)
Assert.Contains(message, eventLogEntry.Message);
Assert.Equal(source, eventLogEntry.Source);
- Assert.StartsWith(Environment.MachineName.ToLower(), eventLogEntry.MachineName.ToLower());
+ Assert.StartsWith(Environment.MachineName.ToLowerInvariant(), eventLogEntry.MachineName.ToLowerInvariant());
Assert.Equal(eventLogEntry.TimeWritten, eventLogEntry.TimeGenerated);
}
finally
else
eventLogEntry = WriteLogEntryWithSource(source, type: true, instance: true, category: true);
- //There is some prefix string already attached to the message passed
+ // There is some prefix string already attached to the message passed
Assert.Contains(message, eventLogEntry.Message);
Assert.Equal((short)eventInstance.CategoryId, eventLogEntry.CategoryNumber);
Assert.Equal("(" + eventLogEntry.CategoryNumber + ")", eventLogEntry.Category);
{
Assert.Throws<ArgumentException>(() => eventLog.WriteEntry(message));
}
-
}
[ConditionalTheory(typeof(Helpers), nameof(Helpers.IsElevatedAndSupportsEventLogs))]
{
string source = "Source_" + nameof(WriteEntryWithInvalidType);
eventLog.Source = source;
- Assert.Throws<InvalidEnumArgumentException>(() => eventLog.WriteEntry(message, (EventLogEntryType)7));
+ Assert.Throws<InvalidEnumArgumentException>(() => eventLog.WriteEntry(message, (EventLogEntryType)7)); // 7 is a random number which is not associated with any type in EventLogEntryType
}
}
try
{
EventLog.WriteEvent(source, eventInstance, rawData, null);
- Assert.Equal(EventLog.LogNameFromSourceName(source, "."), "Application");
+ Assert.Equal("Application", EventLog.LogNameFromSourceName(source, "."));
}
finally
{
internal class Helpers
{
public static bool IsElevatedAndSupportsEventLogs { get => AdminHelpers.IsProcessElevated() && PlatformDetection.IsNotWindowsNanoServer; }
+ public static bool SupportsEventLogs { get => PlatformDetection.IsNotWindowsNanoServer; }
}
}