From 648c31e13afac787c2d5ffde17153b5cfb2868c5 Mon Sep 17 00:00:00 2001 From: pedrobsaila Date: Sun, 12 Jun 2022 17:35:47 +0200 Subject: [PATCH 1/5] EventLogException is missing the original win32 error code --- .../Diagnostics/Reader/EventLogException.cs | 2 +- .../Diagnostics/Reader/EventLogReaderTests.cs | 21 +++++++++++++------ .../Reader/EventLogSessionTests.cs | 6 ++++-- 3 files changed, 20 insertions(+), 9 deletions(-) diff --git a/src/libraries/System.Diagnostics.EventLog/src/System/Diagnostics/Reader/EventLogException.cs b/src/libraries/System.Diagnostics.EventLog/src/System/Diagnostics/Reader/EventLogException.cs index d8d270cbef7ae2..38f27d68020c84 100644 --- a/src/libraries/System.Diagnostics.EventLog/src/System/Diagnostics/Reader/EventLogException.cs +++ b/src/libraries/System.Diagnostics.EventLog/src/System/Diagnostics/Reader/EventLogException.cs @@ -50,7 +50,7 @@ internal static void Throw(int errorCode) public EventLogException() { } public EventLogException(string message) : base(message) { } public EventLogException(string message, Exception innerException) : base(message, innerException) { } - protected EventLogException(int errorCode) { _errorCode = errorCode; } + protected EventLogException(int errorCode) { _errorCode = errorCode; HResult = errorCode; } public override string Message { diff --git a/src/libraries/System.Diagnostics.EventLog/tests/System/Diagnostics/Reader/EventLogReaderTests.cs b/src/libraries/System.Diagnostics.EventLog/tests/System/Diagnostics/Reader/EventLogReaderTests.cs index 76fd40d1cfe642..6460f4e0c2db7f 100644 --- a/src/libraries/System.Diagnostics.EventLog/tests/System/Diagnostics/Reader/EventLogReaderTests.cs +++ b/src/libraries/System.Diagnostics.EventLog/tests/System/Diagnostics/Reader/EventLogReaderTests.cs @@ -48,14 +48,18 @@ public void WrongPathType_ReverseDirection_Throws(bool useBookmark) if (PlatformDetection.IsWindows7) // Null events in PowerShell log return; var query = new EventLogQuery(null, PathType.FilePath, "*[System[(Level=2)]]") { ReverseDirection = true }; + EventLogException exception = null; if (useBookmark) { - Assert.Throws(() => new EventLogReader(query, bookmark: null)); - Assert.Throws(() => new EventLogReader(query, bookmark: Helpers.GetBookmark("Application", PathType.LogName))); + exception = Assert.Throws(() => new EventLogReader(query, bookmark: null)); + Assert.Equal(15000, exception.HResult); + exception = Assert.Throws(() => new EventLogReader(query, bookmark: Helpers.GetBookmark("Application", PathType.LogName))); + Assert.Equal(15000, exception.HResult); } else { - Assert.Throws(() => new EventLogReader(query)); + exception = Assert.Throws(() => new EventLogReader(query)); + Assert.Equal(15000, exception.HResult); } } @@ -67,14 +71,19 @@ public void WrongPathType_TolerateQueryErrors_Throws(bool useBookmark) if (PlatformDetection.IsWindows7) // Null events in PowerShell log return; var query = new EventLogQuery(null, PathType.FilePath, "*[System[(Level=2)]]") { TolerateQueryErrors = true }; + EventLogException exception = null; + if (useBookmark) { - Assert.Throws(() => new EventLogReader(query, bookmark: null)); - Assert.Throws(() => new EventLogReader(query, bookmark: Helpers.GetBookmark("Application", PathType.LogName))); + exception = Assert.Throws(() => new EventLogReader(query, bookmark: null)); + Assert.Equal(15000, exception.HResult); + exception = Assert.Throws(() => new EventLogReader(query, bookmark: Helpers.GetBookmark("Application", PathType.LogName))); + Assert.Equal(15000, exception.HResult); } else { - Assert.Throws(() => new EventLogReader(query)); + exception = Assert.Throws(() => new EventLogReader(query)); + Assert.Equal(15000, exception.HResult); } } diff --git a/src/libraries/System.Diagnostics.EventLog/tests/System/Diagnostics/Reader/EventLogSessionTests.cs b/src/libraries/System.Diagnostics.EventLog/tests/System/Diagnostics/Reader/EventLogSessionTests.cs index 6618a51c8db7e3..6b5b7999a215bb 100644 --- a/src/libraries/System.Diagnostics.EventLog/tests/System/Diagnostics/Reader/EventLogSessionTests.cs +++ b/src/libraries/System.Diagnostics.EventLog/tests/System/Diagnostics/Reader/EventLogSessionTests.cs @@ -50,7 +50,8 @@ public void ExportLog_InvalidInputCombinations_Throws(bool usingDefaultCtor) Assert.Throws(() => session.ExportLog(null, PathType.LogName, LogName, GetTestFilePath())); Assert.Throws(() => session.ExportLog(LogName, PathType.LogName, LogName, null)); Assert.Throws(() => session.ExportLog(LogName, (PathType)0, LogName, GetTestFilePath())); - Assert.Throws(() => session.ExportLog(LogName, PathType.FilePath, LogName, GetTestFilePath())); + EventLogNotFoundException exception = Assert.Throws(() => session.ExportLog(LogName, PathType.FilePath, LogName, GetTestFilePath())); + Assert.Equal(2, exception.HResult); // Does not throw: session.ExportLog(LogName, PathType.LogName, LogName, GetTestFilePath(), tolerateQueryErrors: true); session.CancelCurrentOperations(); @@ -76,7 +77,8 @@ public void ClearLog_LogNameNullEmptyOrNotExist_Throws() Assert.Throws(() => session.ClearLog(null)); Assert.Throws(() => session.ClearLog(null, backupPath: GetTestFilePath())); Assert.Throws(() => session.ClearLog("")); - Assert.Throws(() => session.ClearLog(logName: nameof(ClearLog_LogNameNullEmptyOrNotExist_Throws))); + EventLogNotFoundException exception = Assert.Throws(() => session.ClearLog(logName: nameof(ClearLog_LogNameNullEmptyOrNotExist_Throws))); + Assert.Equal(15007, exception.HResult); } } From cfd395c2f05a45e3ac04f9a175085380208a9fa3 Mon Sep 17 00:00:00 2001 From: pedrobsaila Date: Mon, 13 Jun 2022 21:04:17 +0200 Subject: [PATCH 2/5] rollback tests --- .../Diagnostics/Reader/EventLogReaderTests.cs | 21 ++++++------------- .../Reader/EventLogSessionTests.cs | 6 ++---- 2 files changed, 8 insertions(+), 19 deletions(-) diff --git a/src/libraries/System.Diagnostics.EventLog/tests/System/Diagnostics/Reader/EventLogReaderTests.cs b/src/libraries/System.Diagnostics.EventLog/tests/System/Diagnostics/Reader/EventLogReaderTests.cs index 6460f4e0c2db7f..76fd40d1cfe642 100644 --- a/src/libraries/System.Diagnostics.EventLog/tests/System/Diagnostics/Reader/EventLogReaderTests.cs +++ b/src/libraries/System.Diagnostics.EventLog/tests/System/Diagnostics/Reader/EventLogReaderTests.cs @@ -48,18 +48,14 @@ public void WrongPathType_ReverseDirection_Throws(bool useBookmark) if (PlatformDetection.IsWindows7) // Null events in PowerShell log return; var query = new EventLogQuery(null, PathType.FilePath, "*[System[(Level=2)]]") { ReverseDirection = true }; - EventLogException exception = null; if (useBookmark) { - exception = Assert.Throws(() => new EventLogReader(query, bookmark: null)); - Assert.Equal(15000, exception.HResult); - exception = Assert.Throws(() => new EventLogReader(query, bookmark: Helpers.GetBookmark("Application", PathType.LogName))); - Assert.Equal(15000, exception.HResult); + Assert.Throws(() => new EventLogReader(query, bookmark: null)); + Assert.Throws(() => new EventLogReader(query, bookmark: Helpers.GetBookmark("Application", PathType.LogName))); } else { - exception = Assert.Throws(() => new EventLogReader(query)); - Assert.Equal(15000, exception.HResult); + Assert.Throws(() => new EventLogReader(query)); } } @@ -71,19 +67,14 @@ public void WrongPathType_TolerateQueryErrors_Throws(bool useBookmark) if (PlatformDetection.IsWindows7) // Null events in PowerShell log return; var query = new EventLogQuery(null, PathType.FilePath, "*[System[(Level=2)]]") { TolerateQueryErrors = true }; - EventLogException exception = null; - if (useBookmark) { - exception = Assert.Throws(() => new EventLogReader(query, bookmark: null)); - Assert.Equal(15000, exception.HResult); - exception = Assert.Throws(() => new EventLogReader(query, bookmark: Helpers.GetBookmark("Application", PathType.LogName))); - Assert.Equal(15000, exception.HResult); + Assert.Throws(() => new EventLogReader(query, bookmark: null)); + Assert.Throws(() => new EventLogReader(query, bookmark: Helpers.GetBookmark("Application", PathType.LogName))); } else { - exception = Assert.Throws(() => new EventLogReader(query)); - Assert.Equal(15000, exception.HResult); + Assert.Throws(() => new EventLogReader(query)); } } diff --git a/src/libraries/System.Diagnostics.EventLog/tests/System/Diagnostics/Reader/EventLogSessionTests.cs b/src/libraries/System.Diagnostics.EventLog/tests/System/Diagnostics/Reader/EventLogSessionTests.cs index 6b5b7999a215bb..6618a51c8db7e3 100644 --- a/src/libraries/System.Diagnostics.EventLog/tests/System/Diagnostics/Reader/EventLogSessionTests.cs +++ b/src/libraries/System.Diagnostics.EventLog/tests/System/Diagnostics/Reader/EventLogSessionTests.cs @@ -50,8 +50,7 @@ public void ExportLog_InvalidInputCombinations_Throws(bool usingDefaultCtor) Assert.Throws(() => session.ExportLog(null, PathType.LogName, LogName, GetTestFilePath())); Assert.Throws(() => session.ExportLog(LogName, PathType.LogName, LogName, null)); Assert.Throws(() => session.ExportLog(LogName, (PathType)0, LogName, GetTestFilePath())); - EventLogNotFoundException exception = Assert.Throws(() => session.ExportLog(LogName, PathType.FilePath, LogName, GetTestFilePath())); - Assert.Equal(2, exception.HResult); + Assert.Throws(() => session.ExportLog(LogName, PathType.FilePath, LogName, GetTestFilePath())); // Does not throw: session.ExportLog(LogName, PathType.LogName, LogName, GetTestFilePath(), tolerateQueryErrors: true); session.CancelCurrentOperations(); @@ -77,8 +76,7 @@ public void ClearLog_LogNameNullEmptyOrNotExist_Throws() Assert.Throws(() => session.ClearLog(null)); Assert.Throws(() => session.ClearLog(null, backupPath: GetTestFilePath())); Assert.Throws(() => session.ClearLog("")); - EventLogNotFoundException exception = Assert.Throws(() => session.ClearLog(logName: nameof(ClearLog_LogNameNullEmptyOrNotExist_Throws))); - Assert.Equal(15007, exception.HResult); + Assert.Throws(() => session.ClearLog(logName: nameof(ClearLog_LogNameNullEmptyOrNotExist_Throws))); } } From 66cb814b478fff00f9c852462fc7d048bc0fe88b Mon Sep 17 00:00:00 2001 From: pedrobsaila Date: Tue, 14 Jun 2022 21:31:34 +0200 Subject: [PATCH 3/5] add unit test --- .../Diagnostics/Reader/EventLogSessionTests.cs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/libraries/System.Diagnostics.EventLog/tests/System/Diagnostics/Reader/EventLogSessionTests.cs b/src/libraries/System.Diagnostics.EventLog/tests/System/Diagnostics/Reader/EventLogSessionTests.cs index 6618a51c8db7e3..da18ce76416efb 100644 --- a/src/libraries/System.Diagnostics.EventLog/tests/System/Diagnostics/Reader/EventLogSessionTests.cs +++ b/src/libraries/System.Diagnostics.EventLog/tests/System/Diagnostics/Reader/EventLogSessionTests.cs @@ -106,5 +106,17 @@ public void ClearLog_LogExists_Success() session.CancelCurrentOperations(); } } + + [ConditionalFact(typeof(Helpers), nameof(Helpers.SupportsEventLogs))] + [SkipOnTargetFramework(TargetFrameworkMonikers.NetFramework)] + public void EventLogExceptionShouldHaveHResultSet() + { + using (var session = new EventLogSession()) + { + EventLogNotFoundException exception = Assert.Throws(() => session.ExportLog(LogName, PathType.FilePath, LogName, GetTestFilePath())); + Assert.Equal(2, exception.HResult); + session.CancelCurrentOperations(); + } + } } } From 9fe706cb868e3b071987bcfac5a3411f3d2287c7 Mon Sep 17 00:00:00 2001 From: pedrobsaila Date: Tue, 14 Jun 2022 22:29:33 +0200 Subject: [PATCH 4/5] fix remarks 1 --- .../Windows/Kernel32/Interop.FormatMessage.cs | 6 +-- ...Interop.FormatMessage_SafeLibraryHandle.cs | 4 -- .../src/Resources/Strings.resx | 39 +++++++++++++++++++ .../src/System.Diagnostics.EventLog.csproj | 4 ++ .../Diagnostics/Reader/EventLogException.cs | 33 +++++++++------- .../System.Diagnostics.EventLog.Tests.csproj | 2 + .../Reader/EventLogSessionTests.cs | 2 +- 7 files changed, 68 insertions(+), 22 deletions(-) diff --git a/src/libraries/Common/src/Interop/Windows/Kernel32/Interop.FormatMessage.cs b/src/libraries/Common/src/Interop/Windows/Kernel32/Interop.FormatMessage.cs index f07214f95d0e49..6a0a0c1c931539 100644 --- a/src/libraries/Common/src/Interop/Windows/Kernel32/Interop.FormatMessage.cs +++ b/src/libraries/Common/src/Interop/Windows/Kernel32/Interop.FormatMessage.cs @@ -8,10 +8,10 @@ internal static partial class Interop { internal static partial class Kernel32 { - private const int FORMAT_MESSAGE_IGNORE_INSERTS = 0x00000200; - private const int FORMAT_MESSAGE_FROM_HMODULE = 0x00000800; + public const int FORMAT_MESSAGE_IGNORE_INSERTS = 0x00000200; + public const int FORMAT_MESSAGE_FROM_HMODULE = 0x00000800; private const int FORMAT_MESSAGE_FROM_SYSTEM = 0x00001000; - private const int FORMAT_MESSAGE_ARGUMENT_ARRAY = 0x00002000; + public const int FORMAT_MESSAGE_ARGUMENT_ARRAY = 0x00002000; private const int FORMAT_MESSAGE_ALLOCATE_BUFFER = 0x00000100; private const int ERROR_INSUFFICIENT_BUFFER = 0x7A; diff --git a/src/libraries/Common/src/Interop/Windows/Kernel32/Interop.FormatMessage_SafeLibraryHandle.cs b/src/libraries/Common/src/Interop/Windows/Kernel32/Interop.FormatMessage_SafeLibraryHandle.cs index cc01c8b461b957..2f63f4bf871415 100644 --- a/src/libraries/Common/src/Interop/Windows/Kernel32/Interop.FormatMessage_SafeLibraryHandle.cs +++ b/src/libraries/Common/src/Interop/Windows/Kernel32/Interop.FormatMessage_SafeLibraryHandle.cs @@ -9,10 +9,6 @@ internal static partial class Interop { internal static partial class Kernel32 { - public const int FORMAT_MESSAGE_IGNORE_INSERTS = 0x00000200; - public const int FORMAT_MESSAGE_FROM_HMODULE = 0x00000800; - public const int FORMAT_MESSAGE_ARGUMENT_ARRAY = 0x00002000; - [LibraryImport(Libraries.Kernel32, EntryPoint = "FormatMessageW", SetLastError = true, StringMarshalling = StringMarshalling.Utf16)] public static partial int FormatMessage( int dwFlags, diff --git a/src/libraries/System.Diagnostics.EventLog/src/Resources/Strings.resx b/src/libraries/System.Diagnostics.EventLog/src/Resources/Strings.resx index e0aec25828364b..f9e6ad74722a82 100644 --- a/src/libraries/System.Diagnostics.EventLog/src/Resources/Strings.resx +++ b/src/libraries/System.Diagnostics.EventLog/src/Resources/Strings.resx @@ -239,4 +239,43 @@ Log {0} has already been registered as a source on the local computer. + + Cannot create '{0}' because a file or directory with the same name already exists. + + + The file '{0}' already exists. + + + Unable to find the specified file. + + + Could not find file '{0}'. + + + Could not find a part of the path. + + + Could not find a part of the path '{0}'. + + + The specified file name or path is too long, or a component of the specified path is too long. + + + The path '{0}' is too long, or a component of the specified path is too long. + + + The process cannot access the file '{0}' because it is being used by another process. + + + The process cannot access the file because it is being used by another process. + + + Access to the path is denied. + + + Access to the path is denied. + + + Access to the path '{0}' is denied. + diff --git a/src/libraries/System.Diagnostics.EventLog/src/System.Diagnostics.EventLog.csproj b/src/libraries/System.Diagnostics.EventLog/src/System.Diagnostics.EventLog.csproj index cd2353be08253f..4e97356c5a710e 100644 --- a/src/libraries/System.Diagnostics.EventLog/src/System.Diagnostics.EventLog.csproj +++ b/src/libraries/System.Diagnostics.EventLog/src/System.Diagnostics.EventLog.csproj @@ -110,6 +110,10 @@ System.Diagnostics.EventLog Link="Common\Interop\Windows\Interop.Libraries.cs" /> + + diff --git a/src/libraries/System.Diagnostics.EventLog/src/System/Diagnostics/Reader/EventLogException.cs b/src/libraries/System.Diagnostics.EventLog/src/System/Diagnostics/Reader/EventLogException.cs index 38f27d68020c84..e3a794f1851d04 100644 --- a/src/libraries/System.Diagnostics.EventLog/src/System/Diagnostics/Reader/EventLogException.cs +++ b/src/libraries/System.Diagnostics.EventLog/src/System/Diagnostics/Reader/EventLogException.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. using System.ComponentModel; +using System.IO; using System.Runtime.Serialization; namespace System.Diagnostics.Eventing.Reader @@ -16,30 +17,30 @@ internal static void Throw(int errorCode) { switch (errorCode) { - case 2: - case 3: - case 15007: - case 15027: - case 15028: - case 15002: + case 2: // ERROR_FILE_NOT_FOUND + case 3: // ERROR_PATH_NOT_FOUND + case 15007: // ERROR_EVT_CHANNEL_NOT_FOUND + case 15027: // ERROR_EVT_MESSAGE_NOT_FOUND + case 15028: // ERROR_EVT_MESSAGE_ID_NOT_FOUND + case 15002: // ERROR_EVT_PUBLISHER_METADATA_NOT_FOUND throw new EventLogNotFoundException(errorCode); - case 13: - case 15005: + case 13: // ERROR_INVALID_DATA + case 15005: // ERROR_EVT_INVALID_EVENT_DATA throw new EventLogInvalidDataException(errorCode); case 1818: // RPC_S_CALL_CANCELED is converted to ERROR_CANCELLED - case 1223: + case 1223: // ERROR_CANCELLED throw new OperationCanceledException(); - case 15037: + case 15037: // ERROR_EVT_PUBLISHER_DISABLED throw new EventLogProviderDisabledException(errorCode); - case 5: + case 5: // ERROR_ACCESS_DENIED throw new UnauthorizedAccessException(); - case 15011: - case 15012: + case 15011: // ERROR_EVT_QUERY_RESULT_STALE + case 15012: // ERROR_EVT_QUERY_RESULT_INVALID_POSITION throw new EventLogReadingException(errorCode); default: @@ -50,7 +51,11 @@ internal static void Throw(int errorCode) public EventLogException() { } public EventLogException(string message) : base(message) { } public EventLogException(string message, Exception innerException) : base(message, innerException) { } - protected EventLogException(int errorCode) { _errorCode = errorCode; HResult = errorCode; } + protected EventLogException(int errorCode) + { + _errorCode = errorCode; + HResult = Win32Marshal.MakeHRFromErrorCode(errorCode); + } public override string Message { diff --git a/src/libraries/System.Diagnostics.EventLog/tests/System.Diagnostics.EventLog.Tests.csproj b/src/libraries/System.Diagnostics.EventLog/tests/System.Diagnostics.EventLog.Tests.csproj index f96d3e38fa1a36..53ac60b4ac6f28 100644 --- a/src/libraries/System.Diagnostics.EventLog/tests/System.Diagnostics.EventLog.Tests.csproj +++ b/src/libraries/System.Diagnostics.EventLog/tests/System.Diagnostics.EventLog.Tests.csproj @@ -32,6 +32,8 @@ Link="Common\Interop\Windows\Kernel32\Interop.CloseHandle.cs" /> + (() => session.ExportLog(LogName, PathType.FilePath, LogName, GetTestFilePath())); - Assert.Equal(2, exception.HResult); + Assert.Equal(unchecked((int)0x80070002), exception.HResult); session.CancelCurrentOperations(); } } From b07c35e37cca4181ed0688af86fe5831fd57e87b Mon Sep 17 00:00:00 2001 From: pedrobsaila Date: Thu, 16 Jun 2022 23:22:31 +0200 Subject: [PATCH 5/5] fix remarks 2 --- .../src/Interop/Windows/Interop.Errors.cs | 10 +++++ .../Windows/Kernel32/Interop.FormatMessage.cs | 6 +-- ...Interop.FormatMessage_SafeLibraryHandle.cs | 4 ++ .../src/Resources/Strings.resx | 39 ------------------- .../src/System.Diagnostics.EventLog.csproj | 8 ++-- .../Diagnostics/Reader/EventLogException.cs | 30 +++++++------- .../System.Diagnostics.EventLog.Tests.csproj | 2 - 7 files changed, 35 insertions(+), 64 deletions(-) diff --git a/src/libraries/Common/src/Interop/Windows/Interop.Errors.cs b/src/libraries/Common/src/Interop/Windows/Interop.Errors.cs index 6af8abca7164b0..cde3ae0ac197e8 100644 --- a/src/libraries/Common/src/Interop/Windows/Interop.Errors.cs +++ b/src/libraries/Common/src/Interop/Windows/Interop.Errors.cs @@ -71,6 +71,7 @@ internal static partial class Errors internal const int ERROR_DDE_FAIL = 0x484; internal const int ERROR_DLL_NOT_FOUND = 0x485; internal const int ERROR_NOT_FOUND = 0x490; + internal const int ERROR_CANCELLED = 0x4C7; internal const int ERROR_NETWORK_UNREACHABLE = 0x4CF; internal const int ERROR_NON_ACCOUNT_SID = 0x4E9; internal const int ERROR_NOT_ALL_ASSIGNED = 0x514; @@ -93,6 +94,15 @@ internal static partial class Errors internal const int ERROR_TRUSTED_RELATIONSHIP_FAILURE = 0x6FD; internal const int ERROR_RESOURCE_TYPE_NOT_FOUND = 0x715; internal const int ERROR_RESOURCE_LANG_NOT_FOUND = 0x717; + internal const int RPC_S_CALL_CANCELED = 0x71A; internal const int ERROR_NOT_A_REPARSE_POINT = 0x1126; + internal const int ERROR_EVT_QUERY_RESULT_STALE = 0x3AA3; + internal const int ERROR_EVT_QUERY_RESULT_INVALID_POSITION = 0x3AA4; + internal const int ERROR_EVT_INVALID_EVENT_DATA = 0x3A9D; + internal const int ERROR_EVT_PUBLISHER_METADATA_NOT_FOUND = 0x3A9A; + internal const int ERROR_EVT_CHANNEL_NOT_FOUND = 0x3A9F; + internal const int ERROR_EVT_MESSAGE_NOT_FOUND = 0x3AB3; + internal const int ERROR_EVT_MESSAGE_ID_NOT_FOUND = 0x3AB4; + internal const int ERROR_EVT_PUBLISHER_DISABLED = 0x3ABD; } } diff --git a/src/libraries/Common/src/Interop/Windows/Kernel32/Interop.FormatMessage.cs b/src/libraries/Common/src/Interop/Windows/Kernel32/Interop.FormatMessage.cs index 6a0a0c1c931539..f07214f95d0e49 100644 --- a/src/libraries/Common/src/Interop/Windows/Kernel32/Interop.FormatMessage.cs +++ b/src/libraries/Common/src/Interop/Windows/Kernel32/Interop.FormatMessage.cs @@ -8,10 +8,10 @@ internal static partial class Interop { internal static partial class Kernel32 { - public const int FORMAT_MESSAGE_IGNORE_INSERTS = 0x00000200; - public const int FORMAT_MESSAGE_FROM_HMODULE = 0x00000800; + private const int FORMAT_MESSAGE_IGNORE_INSERTS = 0x00000200; + private const int FORMAT_MESSAGE_FROM_HMODULE = 0x00000800; private const int FORMAT_MESSAGE_FROM_SYSTEM = 0x00001000; - public const int FORMAT_MESSAGE_ARGUMENT_ARRAY = 0x00002000; + private const int FORMAT_MESSAGE_ARGUMENT_ARRAY = 0x00002000; private const int FORMAT_MESSAGE_ALLOCATE_BUFFER = 0x00000100; private const int ERROR_INSUFFICIENT_BUFFER = 0x7A; diff --git a/src/libraries/Common/src/Interop/Windows/Kernel32/Interop.FormatMessage_SafeLibraryHandle.cs b/src/libraries/Common/src/Interop/Windows/Kernel32/Interop.FormatMessage_SafeLibraryHandle.cs index 2f63f4bf871415..cc01c8b461b957 100644 --- a/src/libraries/Common/src/Interop/Windows/Kernel32/Interop.FormatMessage_SafeLibraryHandle.cs +++ b/src/libraries/Common/src/Interop/Windows/Kernel32/Interop.FormatMessage_SafeLibraryHandle.cs @@ -9,6 +9,10 @@ internal static partial class Interop { internal static partial class Kernel32 { + public const int FORMAT_MESSAGE_IGNORE_INSERTS = 0x00000200; + public const int FORMAT_MESSAGE_FROM_HMODULE = 0x00000800; + public const int FORMAT_MESSAGE_ARGUMENT_ARRAY = 0x00002000; + [LibraryImport(Libraries.Kernel32, EntryPoint = "FormatMessageW", SetLastError = true, StringMarshalling = StringMarshalling.Utf16)] public static partial int FormatMessage( int dwFlags, diff --git a/src/libraries/System.Diagnostics.EventLog/src/Resources/Strings.resx b/src/libraries/System.Diagnostics.EventLog/src/Resources/Strings.resx index f9e6ad74722a82..e0aec25828364b 100644 --- a/src/libraries/System.Diagnostics.EventLog/src/Resources/Strings.resx +++ b/src/libraries/System.Diagnostics.EventLog/src/Resources/Strings.resx @@ -239,43 +239,4 @@ Log {0} has already been registered as a source on the local computer. - - Cannot create '{0}' because a file or directory with the same name already exists. - - - The file '{0}' already exists. - - - Unable to find the specified file. - - - Could not find file '{0}'. - - - Could not find a part of the path. - - - Could not find a part of the path '{0}'. - - - The specified file name or path is too long, or a component of the specified path is too long. - - - The path '{0}' is too long, or a component of the specified path is too long. - - - The process cannot access the file '{0}' because it is being used by another process. - - - The process cannot access the file because it is being used by another process. - - - Access to the path is denied. - - - Access to the path is denied. - - - Access to the path '{0}' is denied. - diff --git a/src/libraries/System.Diagnostics.EventLog/src/System.Diagnostics.EventLog.csproj b/src/libraries/System.Diagnostics.EventLog/src/System.Diagnostics.EventLog.csproj index 4e97356c5a710e..dfae87cc90a726 100644 --- a/src/libraries/System.Diagnostics.EventLog/src/System.Diagnostics.EventLog.csproj +++ b/src/libraries/System.Diagnostics.EventLog/src/System.Diagnostics.EventLog.csproj @@ -1,4 +1,4 @@ - + $(NetCoreAppCurrent)-windows;$(NetCoreAppCurrent);$(NetCoreAppMinimum)-windows;$(NetCoreAppMinimum);netstandard2.0;$(NetFrameworkMinimum) true @@ -110,10 +110,8 @@ System.Diagnostics.EventLog Link="Common\Interop\Windows\Interop.Libraries.cs" /> - - + diff --git a/src/libraries/System.Diagnostics.EventLog/src/System/Diagnostics/Reader/EventLogException.cs b/src/libraries/System.Diagnostics.EventLog/src/System/Diagnostics/Reader/EventLogException.cs index e3a794f1851d04..b35991bb87f0bd 100644 --- a/src/libraries/System.Diagnostics.EventLog/src/System/Diagnostics/Reader/EventLogException.cs +++ b/src/libraries/System.Diagnostics.EventLog/src/System/Diagnostics/Reader/EventLogException.cs @@ -17,30 +17,30 @@ internal static void Throw(int errorCode) { switch (errorCode) { - case 2: // ERROR_FILE_NOT_FOUND - case 3: // ERROR_PATH_NOT_FOUND - case 15007: // ERROR_EVT_CHANNEL_NOT_FOUND - case 15027: // ERROR_EVT_MESSAGE_NOT_FOUND - case 15028: // ERROR_EVT_MESSAGE_ID_NOT_FOUND - case 15002: // ERROR_EVT_PUBLISHER_METADATA_NOT_FOUND + case Interop.Errors.ERROR_FILE_NOT_FOUND: + case Interop.Errors.ERROR_PATH_NOT_FOUND: + case Interop.Errors.ERROR_EVT_CHANNEL_NOT_FOUND: + case Interop.Errors.ERROR_EVT_MESSAGE_NOT_FOUND: + case Interop.Errors.ERROR_EVT_MESSAGE_ID_NOT_FOUND: + case Interop.Errors.ERROR_EVT_PUBLISHER_METADATA_NOT_FOUND: throw new EventLogNotFoundException(errorCode); - case 13: // ERROR_INVALID_DATA - case 15005: // ERROR_EVT_INVALID_EVENT_DATA + case Interop.Errors.ERROR_INVALID_DATA: + case Interop.Errors.ERROR_EVT_INVALID_EVENT_DATA: throw new EventLogInvalidDataException(errorCode); - case 1818: // RPC_S_CALL_CANCELED is converted to ERROR_CANCELLED - case 1223: // ERROR_CANCELLED + case Interop.Errors.RPC_S_CALL_CANCELED: + case Interop.Errors.ERROR_CANCELLED: throw new OperationCanceledException(); - case 15037: // ERROR_EVT_PUBLISHER_DISABLED + case Interop.Errors.ERROR_EVT_PUBLISHER_DISABLED: throw new EventLogProviderDisabledException(errorCode); - case 5: // ERROR_ACCESS_DENIED + case Interop.Errors.ERROR_ACCESS_DENIED: throw new UnauthorizedAccessException(); - case 15011: // ERROR_EVT_QUERY_RESULT_STALE - case 15012: // ERROR_EVT_QUERY_RESULT_INVALID_POSITION + case Interop.Errors.ERROR_EVT_QUERY_RESULT_STALE: + case Interop.Errors.ERROR_EVT_QUERY_RESULT_INVALID_POSITION: throw new EventLogReadingException(errorCode); default: @@ -54,7 +54,7 @@ public EventLogException(string message, Exception innerException) : base(messag protected EventLogException(int errorCode) { _errorCode = errorCode; - HResult = Win32Marshal.MakeHRFromErrorCode(errorCode); + HResult = Interop.HRESULT_FROM_WIN32(errorCode); } public override string Message diff --git a/src/libraries/System.Diagnostics.EventLog/tests/System.Diagnostics.EventLog.Tests.csproj b/src/libraries/System.Diagnostics.EventLog/tests/System.Diagnostics.EventLog.Tests.csproj index 53ac60b4ac6f28..f96d3e38fa1a36 100644 --- a/src/libraries/System.Diagnostics.EventLog/tests/System.Diagnostics.EventLog.Tests.csproj +++ b/src/libraries/System.Diagnostics.EventLog/tests/System.Diagnostics.EventLog.Tests.csproj @@ -32,8 +32,6 @@ Link="Common\Interop\Windows\Kernel32\Interop.CloseHandle.cs" /> -