通过从 appsettings.json 读取文件路径来创建文件会自动将我的项目的根目录添加到路径中

srs*_*iko 5 c# asp.net-core asp.net-core-2.2

我正在尝试使用自定义中间件记录来自我的 ASP.NET Core 2.2 API 的异常。在中间件中,我正在从 appsettings.json 读取日志路径

我正在从 appsettings.Developmnet.json 读取这个“ExceptionLogPath”部分并执行以下操作

var dirPath = _config.GetSection("ExceptionLogPath").Value;

            var filename = $"ErrorLog_{DateTime.Now.ToString("yyyyMMdd")}.txt";

            String fullpath = Path.Combine(dirPath, filename);

            if (!File.Exists(fullpath))
            {
                try
                {

                    File.Create(fullpath).Close();
                }
                catch (Exception exx)
                {
                    throw exx;
                }
}
Run Code Online (Sandbox Code Playgroud)

变量 fullPath 具有从配置中读取的确切路径。

但是File.Create(fullPath)会引发异常,因为 fullpath 在运行时将我的项目的相对路径作为前缀附加!因此,系统无法找到创建文件的路径!!

下面是我的 appsettings.Development.json

{
  "Logging": {
    "IncludeScopes": false,
    "LogLevel": {
      "Default": "Debug",
      "System": "Information",
      "Microsoft": "Information"
    }
  },
  "ExceptionLogPath": "?C:\\ExceptionLogs"
}

Run Code Online (Sandbox Code Playgroud)

问题是这只发生在我的项目中。当我的同事在他的 PC 上运行同一个项目时,他能够在指定的路径创建文件。

我想知道为什么当我从 appsettings.json 读取路径时添加了项目的根目录。我能够创建带有硬编码路径值的文件。只有当我从 appsettings.json 读取时它才会失败

编辑 我在尝试在给定路径创建文件时遇到以下异常:

The filename, directory name, or volume label syntax is incorrect : 'E:\Dev\Registry.API\\?C:\ExceptionLogs\ErrorLog_20190926.txt'
Run Code Online (Sandbox Code Playgroud)

如您所见,文件路径的第一部分以我的项目根路径为前缀!