I am trying to log the exceptions from my ASP.NET Core 2.2 API using a custom middleware. Within the middleware I am reading the log path from appsettings.json
I am reading this "ExceptionLogPath" section from appsettings.Developmnet.json and doing the following operations
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;
}
}
The variable fullPath has the exact path as read from configuration.
But File.Create(fullPath)
raises an exception as the fullpath is appending my project's relative path as prefix during runtime! Hence, the system is not able to find the path to create a file!!
Below is my appsettings.Development.json
{
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Debug",
"System": "Information",
"Microsoft": "Information"
}
},
"ExceptionLogPath": "C:\\ExceptionLogs"
}
The problem is this is happening only in my Project. The same project when run by my colleague in his PC, he is able to create the file at the specified path.
I want to know why the project's root directory is added when i read the path from appsettings.json . I am able to create file with hardcoded path values. It only fails when i am reading from appsettings.json
EDIT I am getting the below exception on trying to Create a File at the given path:
The filename, directory name, or volume label syntax is incorrect : 'E:\Dev\Registry.API\\C:\ExceptionLogs\ErrorLog_20190926.txt'
As you can see the first part of the file path is prefixed with my project root path!