Friday, April 30, 2010

Initialising log4net logging for Asp.Net web app

Some snippet below:

protected void Application_Start(object sender, EventArgs e)

log4net.Repository.ILoggerRepository repo = LogManager.GetRepository();
foreach (log4net.Appender.IAppender appender in repo.GetAppenders())
if (appender.Name.CompareTo("RollingFileAppender") == 0 && appender is log4net.Appender.RollingFileAppender)
var appndr = appender as log4net.Appender.RollingFileAppender;
string logPath = "MyApplication.log";
appndr.File = logPath;

logger = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

var configSource = new XmlConfigurationSource(ActiveRecordConfigStream);

Assembly assy = typeof([ParentAssembly]).Assembly;
ActiveRecordStarter.Initialize(configSource, assy.GetTypes());

logger.Info("My Application Starting...");

Tuesday, April 27, 2010

Creating custom .NET Exceptions in C#

public class XyzlException : Exception
public XyzlException (string errorMessage, Exception inner) : base(errorMessage, inner)

You can pass in a custom error message to your new Exception type.

Just try it out...

Thursday, April 22, 2010

Using SQL Server CROSS APPLY to read XML segment

Here is an interesting code snippet:

declare @XML xml
set @XML ='<students>
<student name="Julius">
<subject name="Maths" score="90"></subject>
<subject name="Art" score="50"></subject>
<subject name="English" score="70"></subject>
<student name="Becky">
<subject name="Maths" score="90"></subject>
<subject name="Art" score="50"></subject>
<subject name="English" score="70"></subject>
select ExpressionAlias.student_name,
From @XML.nodes('./students/student') Student (rowset)
Cross Apply
Student.rowset.nodes('./subjects/subject') Subject (rowset)
Cross Apply (
Select Student.rowset.value('@name','NVARCHAR(20)'),
) ExpressionAlias (student_name, subject_name,score)
Order By
ExpressionAlias.student_name ASC,
ExpressionAlias.score DESC;

Sunday, April 11, 2010