Exception thrown in referenced project?
I have a Visual Studio 2010 solution consisting of 2 projects开发者_Python百科:
- Core, a C# class library project which handles the functionality and data access
- UI, an ASP.NET 4 website (.NET Framework 4) that references the Core, and calls functionality in the Core.
My exception handler is set in Global.asax (Application_Error.) When an exception occurs in the UI, everything works perfectly, I get filename, line number, etc.
This is not the case for exceptions that occur in the Core. For this, I get a stacktrace like:
{FillUserCount at offset 2376 in file:line:column <filename unknown>:0:0}
P.S. The Core.dll and Core.pdb are present in the UI Bin folder. In Visual Studio -> Tools -> Options -> Debugging -> "Enable just my code" is unchecked and "Enable source server support" is checked.
Is there a way to get stackframe info (filename, class, method, line number) also for errors that occured in my referenced project ?
The solution to get this working was to create a new web site and re-referencing the Core project.
Still, the prerequisites that Jon Skeet mentioned remain the same:
- the projects have to be built in Debug configuration, and not Release
- while referencing a project, make sure PDB files are copied
- on code side, when retrieving info about the exception that occured, and using StackTrace, make sure you create the instance with the second parameter set to true (true to capture the file name, line number, and column number; otherwise, false.)
This is my final working code. It may help others:
Exception ex = Server.GetLastError().GetBaseException();
StackTrace trace = new StackTrace(ex, true);
if (trace.FrameCount == 0)
return;
StackFrame stackFrame = trace.GetFrame(0);
string className, fileName, functionName, message;
int line = 0;
// if for some reason, filename is being retrieved as null
if (stackFrame.GetFileName() == null)
{
className = ex.TargetSite.ReflectedType.Name;
fileName = ex.TargetSite.ReflectedType.Name;
functionName = ex.TargetSite.Name;
message = ex.Message;
}
else
{
// Collect data where exception occured
string[] splitFile = stackFrame.GetFileName().Split('\\');
className = splitFile[splitFile.Length - 1];
fileName = stackFrame.GetFileName();
functionName = stackFrame.GetMethod().Name;
message = ex.Message;
line = stackFrame.GetFileLineNumber();
}
How are you referencing Core? If you've added a reference to the "Release" DLL, that would explain it... if you've just added a reference to the project and you're running a build with the "Debug" configuration then it should be okay.
精彩评论