I have a non-interactive application where I need to load some string fields before a log file is instantiated. There is a method that reads the contents of a text file into a field. If the path does not exist, I want the exception in the application event log to list the path which was attempted to be accessed. Here's what I tried:
try
{
string contents = File.ReadAllText(path);
}
catch (FileNotFoundException)
{
throw new FileNotFoundException(string.Format("Path not found: {0}",path));
}
When I run my test application with this code, the exception text in the console window is as expected:
Unhandled Exception: System.IO.FileNotFoundException: Path not found: C:\temp\notthere.txt
However, the exception details recorded in the event log do not include Exception message text:
Application: ConsoleApp3.exe
Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.IO.FileNotFoundException
at ConsoleApp3.Program.Main(System.String[])
Is it possible to get .NET to log more exception details automatically, or do I just need to add code to write to the event log myself?