Michells Mesterværk

Log dine exceptions fra Global.asax

by Administrator 5. November 2009 11:29

Hvorfor ikke lade din globale applikationsfil håndtere alt din exception logging ? Ved hjælp af lidt lækkert LINQ og noget XML kan du nemt gemme dine exceptions til en simpel log. Det er vigtigt at du ikke bare “kaster en exception” da exceptions er resourcemæssigt dyre. Det er altid en god idé at logge sine exceptions, så man nemlig nemmere kan finde mulige fejl og rette dem. Følgende skal du ligge ind i Global.asax filen:

void Application_Error(object sender, EventArgs e)
{
Exception[] exceptions = HttpContext.Current.AllErrors;

if (exceptions != null)
{
if (exceptions.Count() > 0)
{
XDocument logFile = new XDocument(
new XElement("logfile", new XAttribute("date", DateTime.Now.Date),
from execp in exceptions
select new XElement("error", execp.InnerException)));

String filename = "/ErrorLogs/" + String.Format("{0:d}", DateTime.Now.Date) + ".xml";

FileInfo todaysLog = new FileInfo(
HttpContext.Current.Server.MapPath(filename)
);

if (!todaysLog.Exists)
{
logFile.Save(
HttpContext.Current.Server.MapPath(filename)
);
}
else
{
XDocument doc = XDocument.Load(HttpContext.Current.Server.MapPath(filename));
doc.Element("logfile").Add(from execp in exceptions
select new XElement("error", execp.InnerException));
doc.Save(
HttpContext.Current.Server.MapPath(filename)
);
}
}
}
}

Du kan teste det ved at “kaste en exception”.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
int d = DividedByZero(2, 0);
}

private Int32 DividedByZero(Int32 number, Int32 number2)
{
return (number / number2);
}
}
}

Resultatet er en daglig logfil.

<?xml version="1.0" encoding="utf-8"?>
<logfile date="2009-11-04T00:00:00+01:00">
<error>
System.DivideByZeroException: Attempted to divide by zero.
at WebApplication1._Default.DividedByZero(Int32 number, Int32
number2) in C:\Users\danielmf\Desktop\Kode\WebApplication1\WebApplication1\Default.aspx.cs:
line 19
....
....
....
</error>
</logfile>

Tags:

Comments

11/5/2009 12:40:03 PM #

Jesper Kamstrup Linnet

Men hvorfor dog kode det selv? Logging frameworks som f.eks. log4net gør fint arbejdet med håndtering af log til forskellige medier (f.eks. filer) og i forskellige formater (f.eks. XML).

Alternativt er der frameworks som ELMAH (http://elmah.googlecode.com/) som tager sig af alt det grove i forbindelse med logning fra ASP.NET.

Jesper Kamstrup Linnet Denmark

11/6/2009 11:19:25 AM #

Daniel

@jesper du har ret i at der findes gode logging frameworks. Men jeg er fan "if it's not broken, fix it" Smile

Daniel Denmark

2/14/2010 1:38:54 PM #

اخبار

Thank you very much for the possibility to have a look into the office. Nice boy, he has got the talent to explain.

اخبار Egypt

2/28/2010 3:14:47 AM #

stock broker

Awesome blog it's not often that I comment but I felt you deserve it.

stock broker United States

2/28/2010 11:31:03 AM #

money

Yes, I have been looking for this all week better late than never!

money United States

3/4/2010 6:21:57 AM #

free hd movies

I enjoy watching movies online, it is way cheaper than going to the theaters.

free hd movies United States

3/9/2010 8:42:23 PM #

WoW Mobile

Do you know which mobile phone services allow tethering?

WoW Mobile United States

3/10/2010 12:00:39 AM #

WoW Mobile

Do you know which cell phone services allow tethering?

WoW Mobile United States

3/14/2010 11:23:40 PM #

ssk sorgulama

This is a really good read for me, Must admit that you are one of the best bloggers I ever saw.Thanks for posting this informative article.

ssk sorgulama United States

3/17/2010 11:30:31 AM #

arac sorgulama

I admire the valuable information you offer in your articles. I will bookmark your blog and have my children check up here often. I am quite sure they will learn lots of new stuff here than anybody else!

arac sorgulama United States

3/20/2010 3:22:47 PM #

ehliyet

Excellent post.I want to thank you for this informative read, I really appreciate sharing this great post. Keep up your work.

ehliyet Denmark

4/6/2010 8:00:26 PM #

horde leveling guide

Admiring the time and effort you put into your blog and detailed information you offer! I will bookmark your blog and have my children check up here often. Thumbs up!

horde leveling guide United States

4/7/2010 4:02:00 AM #

Morgan Zieler

Hello - I must say, I’m impressed with your site. I had no trouble navigating through all the tabs and information was very easy to access. I found what I wanted in no time at all. Pretty awesome. Would appreciate it if you add forums or something, it would be a perfect way for your clients to interact. Great job

Morgan Zieler United States

4/7/2010 8:03:27 PM #

canon ef 50mm f/1.8 ii camera lens

While this subject can be very touchy for most people, my opinion is that there has to be a middle or common ground that we all can find. I do appreciate that youve added relevant and intelligent commentary here though. Thank you!

canon ef 50mm f/1.8 ii camera lens United States

4/7/2010 10:31:15 PM #

coffee machines

I have recently took up my individual blog and so its gratifying to pickup tips from what you have going here. Umpteen Thanks.

coffee machines United States

Add comment


(Will show your Gravatar icon)

(De 3 specielle karaktere i det danske alfabet?)
  Country flag

biuquote
  • Comment
  • Preview
Loading



Powered by BlogEngine.NET 1.6.0.0
Theme by Mads Kristensen