I want to deploy a .NET 5 Web API project to my local IIS. The project includes a Logger.cs file, which writes exception messages caught in controller methods to a text file located in the root of the project. The intention is to check if the LogFile.txt exists and create it if it doesn't, or write messages to the file if it does.
The project works locally, creating a logfile in the path TestWebAPI\TestWebAPI\bin\Debug\net5.0\Logs\LogFile.txt. However, when attempting to publish the project to my local IIS, this functionality is not working.
Below is the code for the Logger.cs file:
using System;
using System.IO;
public static class Logger
{
private static readonly object lockObject = new object();
private static string logFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Logs", "LogFile.txt");
static Logger()
{
EnsureLogFileExists();
}
private static void EnsureLogFileExists()
{
//for thread safety
lock (lockObject)
{
try
{
string directoryPath = Path.GetDirectoryName(logFilePath);
if (!Directory.Exists(directoryPath))
{
Directory.CreateDirectory(directoryPath);
}
if (!File.Exists(logFilePath))
{
File.Create(logFilePath).Close();
}
}
catch (Exception ex)
{
Console.WriteLine($"Error creating log file: {ex.Message}");
}
}
}
public static void Log(string message, Exception ex = null)
{
EnsureLogFileExists();
// For thread safety
lock (lockObject)
{
try
{
using (StreamWriter writer = new StreamWriter(logFilePath, true))
{
string logEntry = $"Time: {DateTime.Now:yyyy-MM-dd HH:mm:ss}";
writer.WriteLine($"Error Message - {message}");
if (ex != null)
{
logEntry += $"\nException Details:\n{ex.ToString()}";
}
writer.WriteLine(logEntry);
writer.Write(" ");
writer.WriteLine("---End of exception-------------------------------------------------------------------------------------------------------------------------");
}
}
catch (Exception logException)
{
Console.WriteLine($"Error writing to log file: {logException.Message}");
}
}
}
}
Here is a sample method that uses the static Log method:
[HttpGet(template: nameof(ThrowError))]
public IActionResult ThrowError()
{
try
{
//will throw an out of range error
int[] numbers = { 1, 2, 3 };
int value = numbers[10];
}
catch (Exception ex)
{
//log the error to the text file
Logger.Log(ex.Message.ToString(), ex);
}
return BadRequest("An error occurred. Check the log for details.");
}
The endpoint of this method is accessible when deployed, but the logs are not being created. Any guidance on what steps I need to take for a successful deployment to IIS would be greatly appreciated. Thank you!
This is the process I follow to deploy the API locally in IIS:
Create a new application pool with the "No Managed Code" CLR option. Create a new site and assign the previously created application pool to it. Publish the Web API project to a directory, and then set the physical path of the newly created site to that location. Attempt to browse it in a web browser. The other controller endpoints are working, confirming that the Web API is deployed correctly. However, I am unsure how to configure file paths in IIS.
I have manually attempted to create a "Logs/LogFile.txt" folder and file in the published folder. But I am still unable to capture logs to the text file. Please help me or let me know where what should I look into to resolve this issue.