public void ClearLog_LogExists_Success() { using (var session = new EventLogSession()) { string log = "Log_" + nameof(ClearLog_LogExists_Success); string source = "Source_" + nameof(ClearLog_LogExists_Success); try { EventLog.CreateEventSource(source, log); using (EventLog eventLog = new EventLog()) { eventLog.Source = source; Helpers.Retry(() => eventLog.WriteEntry("Writing to event log.")); Assert.NotEqual(0, Helpers.Retry((() => eventLog.Entries.Count))); session.ClearLog(logName: log); Assert.Equal(0, Helpers.Retry((() => eventLog.Entries.Count))); } } finally { EventLog.DeleteEventSource(source); } session.CancelCurrentOperations(); } }
public void SetProperties_SaveChanges_NotAdmin_Throws() { const string LogName = "Application"; using (var session = new EventLogSession()) { EventLogConfiguration configuration = null; try { configuration = new EventLogConfiguration(LogName, session); } catch (EventLogNotFoundException) { configuration?.Dispose(); return; } configuration.IsEnabled = false; configuration.SecurityDescriptor = string.Empty; configuration.LogFilePath = null; configuration.LogMode = EventLogMode.Retain; configuration.ProviderLevel = 1; configuration.ProviderKeywords = 1; configuration.MaximumSizeInBytes = long.MaxValue; Assert.Throws <UnauthorizedAccessException>(() => configuration.SaveChanges()); configuration.Dispose(); session.CancelCurrentOperations(); } }
public void ProviderNameTests(bool noProviderName) { string log = "Application"; string source = "Source_" + nameof(ProviderNameTests); using (var session = new EventLogSession()) { try { EventLog.CreateEventSource(source, log); string providerName = noProviderName ? "" : source; using (var providerMetadata = new ProviderMetadata(providerName)) { Assert.Null(providerMetadata.DisplayName); Assert.Equal(providerName, providerMetadata.Name); Assert.Equal(new Guid(), providerMetadata.Id); Assert.Empty(providerMetadata.Events); Assert.Empty(providerMetadata.Keywords); Assert.Empty(providerMetadata.Levels); Assert.Empty(providerMetadata.Opcodes); Assert.Empty(providerMetadata.Tasks); Assert.NotEmpty(providerMetadata.LogLinks); if (!string.IsNullOrEmpty(providerName)) { foreach (var logLink in providerMetadata.LogLinks) { Assert.True(logLink.IsImported); Assert.Equal(log, logLink.LogName); Assert.NotEmpty(logLink.DisplayName); if (CultureInfo.CurrentCulture.Name.Split('-')[0] == "en") { Assert.Equal("Application", logLink.DisplayName); } else if (CultureInfo.CurrentCulture.Name.Split('-')[0] == "es") { Assert.Equal("Aplicación", logLink.DisplayName); } } Assert.Contains("EventLogMessages.dll", providerMetadata.MessageFilePath); Assert.Contains("EventLogMessages.dll", providerMetadata.HelpLink.ToString()); } else { Assert.Null(providerMetadata.MessageFilePath); Assert.Null(providerMetadata.HelpLink); } Assert.Null(providerMetadata.ResourceFilePath); Assert.Null(providerMetadata.ParameterFilePath); } } finally { EventLog.DeleteEventSource(source); } session.CancelCurrentOperations(); } }
public void EventLogExceptionShouldHaveHResultSet() { using (var session = new EventLogSession()) { EventLogNotFoundException exception = Assert.Throws <EventLogNotFoundException>(() => session.ExportLog(LogName, PathType.FilePath, LogName, GetTestFilePath())); Assert.Equal(unchecked ((int)0x80070002), exception.HResult); session.CancelCurrentOperations(); } }
public void ProviderNameTests(bool noProviderName) { if (PlatformDetection.IsWindows10Version22000OrGreater) // ActiveIssue("https://github.com/dotnet/runtime/issues/58829") { return; } string log = "Application"; string source = "Source_" + nameof(ProviderNameTests); using (var session = new EventLogSession()) { try { EventLog.CreateEventSource(source, log); string providerName = noProviderName ? "" : source; using (var providerMetadata = new ProviderMetadata(providerName)) { Assert.Null(providerMetadata.DisplayName); Assert.Equal(providerName, providerMetadata.Name); Assert.Equal(new Guid(), providerMetadata.Id); Assert.Empty(providerMetadata.Events); Assert.Empty(providerMetadata.Keywords); Assert.Empty(providerMetadata.Levels); Assert.Empty(providerMetadata.Opcodes); Assert.Empty(providerMetadata.Tasks); Assert.NotEmpty(providerMetadata.LogLinks); if (!string.IsNullOrEmpty(providerName)) { foreach (var logLink in providerMetadata.LogLinks) { Assert.True(logLink.IsImported); Assert.Equal(log, logLink.LogName); Assert.NotEmpty(logLink.DisplayName); if (CultureInfo.CurrentCulture.Name.Split('-')[0] == "en") { Assert.Equal("Application", logLink.DisplayName); } else if (CultureInfo.CurrentCulture.Name.Split('-')[0] == "es") { Assert.Equal("Aplicaci\u00F3n", logLink.DisplayName); } } string[] expectedMessageFileNames = new[] { "EventLogMessages.dll", "System.Diagnostics.EventLog.Messages.dll" }; string messageFileName = Path.GetFileName(providerMetadata.MessageFilePath); Assert.Contains(expectedMessageFileNames, expected => expected.Equals(messageFileName, StringComparison.OrdinalIgnoreCase)); if (providerMetadata.HelpLink != null) { string helpLink = providerMetadata.HelpLink.ToString(); Assert.Contains(expectedMessageFileNames, expected => - 1 != helpLink.IndexOf(expected, StringComparison.OrdinalIgnoreCase)); } } else { Assert.Null(providerMetadata.MessageFilePath); Assert.Null(providerMetadata.HelpLink); } Assert.Null(providerMetadata.ResourceFilePath); Assert.Null(providerMetadata.ParameterFilePath); } } finally { EventLog.DeleteEventSource(source); } session.CancelCurrentOperations(); } }