I have this piece of code in a C# program:
ListOfCabins();
ListOfWoodTypes();
The first function looks as follows:
private void ListOfCabins()
{
Logger.Info($"Start function ListOfCabins()");
try
{
}
catch (Exception ex)
{
...
}
Logger.Info($"End of function ListOfCabins()");
}
The other function goes as follows:
private void ListOfWoodTypes()
{
Logger.Info($"Start function ListOfWoodTypes()");
...
Logger
is a typical NLog
object:
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
In the logs, I see:
End of function ListOfCabins()
I do not see:
Start function ListOfWoodTypes()
My application crashes, in the event log I see:
Application: ...
Framework Version: v4.0.30319
Description: The process wa terminated due to an unhandled exception.
Exception Info: System.IO.FileNotFoundException
at ...ListOfWoodTypes()
at ...
I have different questions here:
- A
System.IO.FileNotFoundException
, does this mean that my application is ok, tries to open some external file which is not present, or is it something like a Java process, missing a*.class
file? - Why is a
System.IO.FileNotFoundException
not catched by mytry ... catch (Exception ex)
clause? - What about the logs? I'm expecting to see at least the first line of the
ListOfWoodTypes()
function. Does this mean that there is some buffering inNLog
technology, causingNLog
logging to have some delay on a crash?
Edit1, debugging not possible
I'm developping this program on my own PC, I build it here, but I then copy the executable to my customer's PC, so debugging is not possible.
CodePudding user response:
Let me start - this is not as easy as it sounds. The devil is, as they say, in the details...
System.IO.FileNotFoundException
This generally means SOMEONE has thrown it. Period. It is MEANT to be thrown for a file not being found, but I have seen system exceptions abused by stupid programmers, or "file not found" being interpreted quite complicated (network streams CAN be opened using a file api) at times. So, while it is an indication of - you know - a file not found, it is not 100% sure that this is what triggered it.
Why is a System.IO.FileNotFoundException not catched by my try ... catch (Exception ex) clause?
You tell us. It SHOULD be - IF it is thrown within it.
Your own code has logger information before and after. And logging may go into - guess what - a file. It is not totally surreal to assume that possibly the first line throws the error.
WE can not know - you should. In the debugger make a break when this exception is thrown. Get the exact location it is thrown. Look at the call stack. LOOK AT THE PROPERTIES. Exceptions often have additional data in them - message, possibly properties. Just telling us the details is like "a mercedes crashed, what was the error". The debugging details would likely answer all your questions.
What about the logs? I'm expecting to see at least the first line of the ListOfWoodTypes() function.
Yeah, except if THAT LINE THROWS THE EXCEPTION. With no log file I would strongly suggest - you know - logging being the problem.
Does this mean that there is some buffering in NLog technology, causing NLog logging to have some delay on a crash?
Well, that technology being "use standard .NET caching"... it would be utterly stupid NOT to use it. See, the problem being that if you do not cache, you need a lot of disci IO, making logging a major performance hog, possibly. You can not have this.
But: Caching or not, is the file there? If not - there goes your implication. File generation is not cached (writes are, but the file has to be there for a stream to be opened). The file not being there...