admin管理员组

文章数量:1336405

FYI to NLog devos:

I have a VS 2022 ASP.NET Core solution that works under .NET 8.0. I updated the TargetFramework to net9.0 and everything works normally. Then I updated all of the NLog.* packages to latest (including NLog.Web.AspNetCore to 5.3.14) and the app broke.

On launch I get a System.UriFormatException from this code:

logger = NLog.LogManager.Setup()
             .LoadConfigurationFromAppSettings().GetCurrentClassLogger();

I troubleshot by going through different package versions and in the end it turned out the breakage happens in taking NLog.Web.AspNetCore from 5.3.11 to 5.3.12.

For now I'm good, as I can stick with 5.3.11, but be aware that there's something latent here you'll need to address.

UPDATE: added nlog-internal.txt content generated by 5.3.12, as requested by @RolfKristensen, below the stack trace. When I back down to 5.3.11, the similar output doesn't end where it does in .12:

2024-11-20 15:41:38.5470 Debug Skip loading NLogLoggingConfiguration from empty config section: NLog
2024-11-20 15:41:38.5834 Info Loading NLog config from XML file: Z:\rwells\very\special\NLog.config
...normal loading proceeds...

In .11 it seems that after NLog gives up on the seemingly hostname-less UNC it proceeds with Z:, which is the drive letter mapped to a UNC where my files are located.

Perhaps there was some change/regression in LoggingConfigurationFileLoader relative to UNC- and mapped-drive-handling from .11 to .12?

UPDATE 2 (2024.11.21): see uri-hacking.ps1 and its output, below everything else.

Cheers,
Richard

Stack Trace

System.UriFormatException  
HResult=0x80131537  
Message=Invalid URI: The hostname could not be parsed.  
Source=System.Private.Uri  

StackTrace:  
at System.Uri.CreateThis(String uri, Boolean dontEscape, UriKind uriKind, UriCreationOptions& creationOptions)  
at System.Uri..ctor(String uriString, UriKind uriKind)  
at System.Xml.XmlResolver.ResolveUri(Uri baseUri, String relativeUri)  
at System.Xml.XmlTextReaderImpl..ctor(String uriStr, XmlReaderSettings settings, XmlParserContext context, XmlResolver uriResolver)  
at System.Xml.XmlReader.Create(String inputUri)  
at NLog.Internal.Fakeables.AppEnvironmentWrapper.LoadXmlFile(String path)  
at NLog.Config.XmlLoggingConfiguration.CreateFileReader(String fileName)  
at NLog.Config.XmlLoggingConfiguration.LoadFromXmlFile(String fileName)  
at NLog.Config.XmlLoggingConfiguration..ctor(String fileName, LogFactory logFactory)  
at  NLog.Web.SetupBuilderExtensions.<>c__DisplayClass0_0.<LoadConfigurationFromAppSettings>b__2(ISetupLoadConfigurationBuilder config)  
at NLog.SetupBuilderExtensions.LoadConfiguration(ISetupBuilder setupBuilder, Action`1 configBuilder)  
at NLog.Web.SetupBuilderExtensions.LoadConfigurationFromAppSettings(ISetupBuilder setupBuilder, String basePath, String environment, String nlogConfigSection, Boolean optional, Boolean reloadOnChange)  
at Program.<Main>$(String[] args) in Z:\rwells\very\special\Program.cs:line 20

nlog-internal.txt

2024-11-20 15:28:06.7547 Debug No file exists at candidate config file location: \\?\UNC\rwells\very\special\bin\Development\net9.0\IDService.exe.nlog
2024-11-20 15:28:06.7547 Debug No file exists at candidate config file location: \\?\UNC\rwells\very\special\bin\Development\net9.0\IDService.dll.nlog
2024-11-20 15:28:06.7779 Debug Reading config from XML file: \\?\UNC\rwells\very\special\bin\Development\net9.0\NLog.config
2024-11-20 15:28:06.8262 Error Failed loading from config file location: \\?\UNC\rwells\very\special\bin\Development\net9.0\NLog.config Exception: System.UriFormatException: Invalid URI: The hostname could not be parsed.
   at System.Uri.CreateThis(String uri, Boolean dontEscape, UriKind uriKind, UriCreationOptions& creationOptions)
   at System.Uri..ctor(String uriString, UriKind uriKind)
   at System.Xml.XmlResolver.ResolveUri(Uri baseUri, String relativeUri)
   at System.Xml.XmlTextReaderImpl..ctor(String uriStr, XmlReaderSettings settings, XmlParserContext context, XmlResolver uriResolver)
   at System.Xml.XmlReader.Create(String inputUri)
   at NLog.Internal.Fakeables.AppEnvironmentWrapper.LoadXmlFile(String path)
   at NLog.Config.LoggingConfigurationFileLoader.LoadXmlLoggingConfigurationFile(LogFactory logFactory, String configFile)
   at NLog.Config.LoggingConfigurationFileLoader.TryLoadLoggingConfiguration(LogFactory logFactory, String configFile, LoggingConfiguration& config)
2024-11-20 15:28:06.8446 Debug No file exists at candidate config file location: \\?\UNC\rwells\very\special\bin\Development\net9.0\NLog.dll.nlog
2024-11-20 15:28:06.8465 Info NLog Configuration has not been loaded.
2024-11-20 15:28:07.3532 Debug No file exists at candidate config file location: \\?\UNC\rwells\very\special\bin\Development\net9.0\nlog.Development.config

uri-hacking.ps1

$paths = (
    '\\sharepoint.business\DavWWWRoot\rs\project 1\document.txt',
    'Z:\rwells\very\special\place',
    '\\actual-hostname\actual-sharename\rwells\very\special\place',
    '\\?\UNC\actual-hostname\actual-sharename\rwells\very\special\place'
)

foreach ($path in $paths) {

    echo "$path ->"
    $uri = [System.Uri]$path
    echo "    $($uri.AbsoluteUri) [is UNC = $($uri.IsUnc)]"

}

output on Window Server 2022

\\sharepoint.business\DavWWWRoot\rs\project 1\document.txt ->
    file://sharepoint.business/DavWWWRoot/rs/project%201/document.txt [is UNC = True]
Z:\rwells\very\special\place ->
    file:///Z:/rwells/very/special/place [is UNC = False]
\\actual-hostname\actual-sharename\rwells\very\special\place ->
    file://actual-hostname/actual-sharename/rwells/very/special/place [is UNC = True]
\\?\UNC\actual-hostname\actual-sharename\rwells\very\special\place ->
InvalidArgument: Z:\rwells\Temp\uri-hacking.ps1:11
Line |
  11 |      $uri = [System.Uri]$path
     |      ~~~~~~~~~~~~~~~~~~~~~~~~
     | Cannot convert value "\\?\UNC\actual-hostname\actual-sharename\rwells\very\special\place" to type "System.Uri". Error: "Invalid URI: The hostname could not be parsed."
    file://actual-hostname/actual-sharename/rwells/very/special/place [is UNC = True]

FYI to NLog devos:

I have a VS 2022 ASP.NET Core solution that works under .NET 8.0. I updated the TargetFramework to net9.0 and everything works normally. Then I updated all of the NLog.* packages to latest (including NLog.Web.AspNetCore to 5.3.14) and the app broke.

On launch I get a System.UriFormatException from this code:

logger = NLog.LogManager.Setup()
             .LoadConfigurationFromAppSettings().GetCurrentClassLogger();

I troubleshot by going through different package versions and in the end it turned out the breakage happens in taking NLog.Web.AspNetCore from 5.3.11 to 5.3.12.

For now I'm good, as I can stick with 5.3.11, but be aware that there's something latent here you'll need to address.

UPDATE: added nlog-internal.txt content generated by 5.3.12, as requested by @RolfKristensen, below the stack trace. When I back down to 5.3.11, the similar output doesn't end where it does in .12:

2024-11-20 15:41:38.5470 Debug Skip loading NLogLoggingConfiguration from empty config section: NLog
2024-11-20 15:41:38.5834 Info Loading NLog config from XML file: Z:\rwells\very\special\NLog.config
...normal loading proceeds...

In .11 it seems that after NLog gives up on the seemingly hostname-less UNC it proceeds with Z:, which is the drive letter mapped to a UNC where my files are located.

Perhaps there was some change/regression in LoggingConfigurationFileLoader relative to UNC- and mapped-drive-handling from .11 to .12?

UPDATE 2 (2024.11.21): see uri-hacking.ps1 and its output, below everything else.

Cheers,
Richard

Stack Trace

System.UriFormatException  
HResult=0x80131537  
Message=Invalid URI: The hostname could not be parsed.  
Source=System.Private.Uri  

StackTrace:  
at System.Uri.CreateThis(String uri, Boolean dontEscape, UriKind uriKind, UriCreationOptions& creationOptions)  
at System.Uri..ctor(String uriString, UriKind uriKind)  
at System.Xml.XmlResolver.ResolveUri(Uri baseUri, String relativeUri)  
at System.Xml.XmlTextReaderImpl..ctor(String uriStr, XmlReaderSettings settings, XmlParserContext context, XmlResolver uriResolver)  
at System.Xml.XmlReader.Create(String inputUri)  
at NLog.Internal.Fakeables.AppEnvironmentWrapper.LoadXmlFile(String path)  
at NLog.Config.XmlLoggingConfiguration.CreateFileReader(String fileName)  
at NLog.Config.XmlLoggingConfiguration.LoadFromXmlFile(String fileName)  
at NLog.Config.XmlLoggingConfiguration..ctor(String fileName, LogFactory logFactory)  
at  NLog.Web.SetupBuilderExtensions.<>c__DisplayClass0_0.<LoadConfigurationFromAppSettings>b__2(ISetupLoadConfigurationBuilder config)  
at NLog.SetupBuilderExtensions.LoadConfiguration(ISetupBuilder setupBuilder, Action`1 configBuilder)  
at NLog.Web.SetupBuilderExtensions.LoadConfigurationFromAppSettings(ISetupBuilder setupBuilder, String basePath, String environment, String nlogConfigSection, Boolean optional, Boolean reloadOnChange)  
at Program.<Main>$(String[] args) in Z:\rwells\very\special\Program.cs:line 20

nlog-internal.txt

2024-11-20 15:28:06.7547 Debug No file exists at candidate config file location: \\?\UNC\rwells\very\special\bin\Development\net9.0\IDService.exe.nlog
2024-11-20 15:28:06.7547 Debug No file exists at candidate config file location: \\?\UNC\rwells\very\special\bin\Development\net9.0\IDService.dll.nlog
2024-11-20 15:28:06.7779 Debug Reading config from XML file: \\?\UNC\rwells\very\special\bin\Development\net9.0\NLog.config
2024-11-20 15:28:06.8262 Error Failed loading from config file location: \\?\UNC\rwells\very\special\bin\Development\net9.0\NLog.config Exception: System.UriFormatException: Invalid URI: The hostname could not be parsed.
   at System.Uri.CreateThis(String uri, Boolean dontEscape, UriKind uriKind, UriCreationOptions& creationOptions)
   at System.Uri..ctor(String uriString, UriKind uriKind)
   at System.Xml.XmlResolver.ResolveUri(Uri baseUri, String relativeUri)
   at System.Xml.XmlTextReaderImpl..ctor(String uriStr, XmlReaderSettings settings, XmlParserContext context, XmlResolver uriResolver)
   at System.Xml.XmlReader.Create(String inputUri)
   at NLog.Internal.Fakeables.AppEnvironmentWrapper.LoadXmlFile(String path)
   at NLog.Config.LoggingConfigurationFileLoader.LoadXmlLoggingConfigurationFile(LogFactory logFactory, String configFile)
   at NLog.Config.LoggingConfigurationFileLoader.TryLoadLoggingConfiguration(LogFactory logFactory, String configFile, LoggingConfiguration& config)
2024-11-20 15:28:06.8446 Debug No file exists at candidate config file location: \\?\UNC\rwells\very\special\bin\Development\net9.0\NLog.dll.nlog
2024-11-20 15:28:06.8465 Info NLog Configuration has not been loaded.
2024-11-20 15:28:07.3532 Debug No file exists at candidate config file location: \\?\UNC\rwells\very\special\bin\Development\net9.0\nlog.Development.config

uri-hacking.ps1

$paths = (
    '\\sharepoint.business\DavWWWRoot\rs\project 1\document.txt',
    'Z:\rwells\very\special\place',
    '\\actual-hostname\actual-sharename\rwells\very\special\place',
    '\\?\UNC\actual-hostname\actual-sharename\rwells\very\special\place'
)

foreach ($path in $paths) {

    echo "$path ->"
    $uri = [System.Uri]$path
    echo "    $($uri.AbsoluteUri) [is UNC = $($uri.IsUnc)]"

}

output on Window Server 2022

\\sharepoint.business\DavWWWRoot\rs\project 1\document.txt ->
    file://sharepoint.business/DavWWWRoot/rs/project%201/document.txt [is UNC = True]
Z:\rwells\very\special\place ->
    file:///Z:/rwells/very/special/place [is UNC = False]
\\actual-hostname\actual-sharename\rwells\very\special\place ->
    file://actual-hostname/actual-sharename/rwells/very/special/place [is UNC = True]
\\?\UNC\actual-hostname\actual-sharename\rwells\very\special\place ->
InvalidArgument: Z:\rwells\Temp\uri-hacking.ps1:11
Line |
  11 |      $uri = [System.Uri]$path
     |      ~~~~~~~~~~~~~~~~~~~~~~~~
     | Cannot convert value "\\?\UNC\actual-hostname\actual-sharename\rwells\very\special\place" to type "System.Uri". Error: "Invalid URI: The hostname could not be parsed."
    file://actual-hostname/actual-sharename/rwells/very/special/place [is UNC = True]
Share Improve this question edited Dec 3, 2024 at 1:28 Zhi Lv 21.7k1 gold badge27 silver badges37 bronze badges asked Nov 20, 2024 at 19:35 Richard WellsRichard Wells 216 bronze badges 15
  • 1 Can you enable NLog InternalLogger from code before calling NLog.LogManager.Setup() ? And then update your question to also include the output from NLog InternalLogger using LogLevel = Debug ? – Rolf Kristensen Commented Nov 20, 2024 at 19:42
  • @RolfKristensen: Neat trick, thanks. (I, of course, wasn't seeing anything in nlog-internal.txt because this exception was thrown before my nlog.config directives for internal logging were parsed. I'll edit the base question with the result. – Richard Wells Commented Nov 20, 2024 at 20:35
  • \\?\UNC\rwells\very\special\bin\Development\net9.0\NLog.config is a very strange FilePath. What is the output from System.Environment.CurrentDirectory and output from System.AppContext.BaseDirectory ? Maybe some new kind of network-file-share protection? – Rolf Kristensen Commented Nov 20, 2024 at 20:58
  • Interesting, seems it tries to use UNC-file-share-path: stackoverflow/a/36196538/193178 (Instead of the network-drive), and UNC doesn't seem to be a valid Uri-value. – Rolf Kristensen Commented Nov 20, 2024 at 21:03
  • Here are the values of those properties: 2024-11-21 09:09:17.4953 Debug System.Environment.CurrentDirectory: G:\rwells\very\special 2024-11-21 09:09:17.4953 Debug System.AppContext.BaseDirectory: \\?\UNC\actual-hostname\actual-sharename\rwells\very\special\bin\Development\net9.0\ Notes: 1. You probably figured out that I masked the real path with rwells\very\special. 2. In this case, the UNC in System.AppContext.BaseDirectory doesn't start with rwells..., as it did before. Real hostname and sharename (also masked), show as first 2 path items after UNC. – Richard Wells Commented Nov 21, 2024 at 14:12
 |  Show 10 more comments

1 Answer 1

Reset to default 0

NLog.Web.AspNetCore v5.4 has been released, that should handle the breaking change in NET9 with returning Long UNC when starting from network-drive.

.NET 9.0.3 should include a fix, so only returning Long UNC when necessary. See also: https://github/dotnet/runtime/pull/111499

本文标签: cNLogWebAspNetCore 5312 raises SystemUriFormatException with ltTargetFrameworkgt net90Stack Overflow