简体   繁体   中英

What is the best way to log exceptions in Visual Basic, on an ASP.NET Application?

I'm working on an ASP.NET web application, written using Visual Basic, and I'm trying to track down an error message that I'm getting.

I'd like to get it to log the exception to a file (or the event log) so I can see it, as the error only occurs on the production server, and not on the development environment (therefore VS isn't installed on there...).

Does anyone have any thoughts as to best practice as to how to do this ?

Cheers

不确定它是否是“最佳实践”,但最常见的方法是使用log4net

Use log4net or Enterprise Library Logging . I'd also add an http module which logs all unhandled errors (Filter out 404 errors).

When doing a global error handler, you will probally want to develope some way to know that you've already logged an error. This happens if you want to log an error closer to the source of the error allowing you to include more context into what went wrong.

In my case when I log any errors, I add an item tot he Data collection on an exception. My global error handler then inspects all exception objects verifying we haven't logged any of them.

Surprised no one has mentioned Elmah yet

Elmah is specially built for error logging with ASP.NET, and one of the great things about it, is that you can add it to an ASP.NET app just by dropping the DLL in the Bin directory, and making some edits to the web.config file. No code changes are necessary.

If you're running ASP.NET 2.0 or above, the best thing is to do nothing. By default, ASP.NET Health Monitoring will log exceptions to the Application event log. Just use eventvwr.exe and look for warnings from "ASP.NET". The information it logs is very complete.

When I say "do nothing", I mean don't catch exceptions if you're only going to log them. You can set a custom error page if you like, but make sure the exceptions are not caught at the top level.

Messy: when it comes up, catch it (assuming you can?) and drop as much data out to the PAGE as you can. Put it in an HTML comment so the user can't see it. Something like:

lblError.Text = (put the formatted Exception info here);

< ! -- < asp:label id=lblError /> -->

Almost as messy: look up the Trace.WriteLine kinda stuff. You can enable tracing in ASP.NET which allows you to go to a specific page (_trace.axd?) and get a dump of all the messages for that page load. I've not really used it, but it does work.

A little less messy (as this appears to be a temporary thing): use Trace.WriteLine to output stuff, and run debugview (from microsoft) on the server, if you have physical access. You can then capture this debug output (the core win32 api on this is OutputDebugString - I think the .net call is Debug.WriteLine)

Nicer: Log4Net. Set it up so you can use it anywhere. Put it on "ERROR" most of the time, but in this case, use "DEBUG" until you find the solution.

I'd go for 3, then 4. Or 3 now, and 4 the next time you clean up the codebase a little.

:)

If you can get hold of the global.asax for the site - Then in the 'Application_Error' section of the Global.asax do something like this

Dim ex As New Exception() ex = Server.GetLastError().GetBaseException() Dim g As New generalfunctions ASendEmailFunction(ex.Message.ToString() & ex.Source.ToString() & ex.StackTrace.ToString())

This will get every error of the application and send you all the details - You need to create that ASendEmailFunction() that just sends you an email with the contents

(You could do it alot better than this but I'm typing this directly into this box)

The most common used libraries for logging are the enterprise library logging block or log4Net: -Enterprise Library Logging Block article: http://www.codeproject.com/KB/architecture/GetLoggingWithEntLib.aspx -Log4Net: http://www.ondotnet.com/pub/a/dotnet/2003/06/16/log4net.html

Cheers

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM