ITmeze - IT world server like cyprus meze

Recent blog posts Tagged 'net'

Manually forcing ELMAH to log exception

Itmeze

ELMAH is one of those projects that I use for literally ALL my own projects. It is an easy to implement error-logging component - for ASP.NET (MVC as well). The best thing about it is that it comes with web page that allows you to view all logged errors (even those of 'yellow screen of death'), plus email notification or rss feed. wow ?! Okay, so ELMAH logs all of our unhanded exceptions... What about the case when you 'catch' exception but would still like to log it in ELAM defined medium. To do that, use code sample below:

try {
    throw new ApplicationException("This error has to be logged");
} catch (Exception e) {
    Elmah.ErrorSignal.FromCurrentContext().Raise(e);
}

Such a call is the most proper one and is much safer than calling Elmah.ErrorLog.Default.Log(e). Happy coding!

Tags: [.net] [elmah] [log] [web development]

LINQ Enumerable.Agregate with Path.Combine

Itmeze

Path.Combine accepts two parameters. Just two... So when we combine multiple path we have to deal with with nesting of Path.Combine:

string rootDir = "/root/app";
string folder1 = "users";
string folder2 = "mike/upload";
string filename = "text.xml";

var path = Path.Combine(rootDir, Path.Combine(folder1, Path.Combine(folder2, filename)));

There are 2 ways to handle this: 1. Use Enumerable.Agregate:

string rootDir = "/root/app";
string folder1 = "users";
string folder2 = "mike/upload";
string filename = "text.xml";

var path2 = new string[] { rootDir, folder1, folder2, filename }.Aggregate(Path.Combine);

2. Or upgrade to .NET 4.0 where there is an overload with Path.Combine(params string[] paths) :) Have fun!

Tags: [.net] [linq] [programming] [web development]

Copyright 2013 © ITmeze