views:

795

answers:

2

I would like to get stack trace (file name and line number) information for logging exceptions etc. in a production environment. The DLLs are installed in the GAC. Is there any way to do this?

This article says about putting PDB files in the GAC:

You can spot these easily because they will say you need to copy the debug symbols (.pdb file) to the GAC. In and of itself, that will not work.

I know this article refers to debugging with VS but I thought it might apply to logging the stacktrace also.

I've followed the instructions for the answer to this question except for unchecking Optimize code which they said was optional.

I copied the dlls and pdbs into the GAC but I'm still not getting the stack trace information. Here's what I get in the log file for the stack trace:

OnAuthenticate at offset 161 in file:line:column <filename unknown>:0:0
ValidateUser at offset 427 in file:line:column <filename unknown>:0:0
LogException at offset 218 in file:line:column <filename unknown>:0:0

I'm using NLog.

My NLog layout is:

layout="${date:format=s}|${level}|${callsite}|${identity}|${message}|${stacktrace:format=Raw}"

${stacktrace:format=Raw} being the relevant part.

+1  A: 

Try raw in lowercase:

   ${stacktrace:format=raw}

If that doesn't work try:

   ${exception:format=stacktrace}
Mitch Wheat
Neither worked though the second one gave me a nicer layout of the method names: CalliHelper.EventArgFunctionCaller => MembershipLogin.Page_Load => MembershipLogin.Configure
Jonathan Parker
A: 

Try this:

StringWriter sw = new StringWriter();
new Throwable().printStackTrace(new PrintWriter(sw));
String stackTrace = sw.toString();

The string stackTrace will have the stackstrace of the current thread

Martins
Thanks, though I don't really want to change the NLog source.
Jonathan Parker