Logging Exception Ent Lib 3.1

Topics: Exception Handling Application Block, Logging Application Block
Dec 6, 2008 at 3:33 PM
Hi,
I am not able to log exception into the database...
Step i have follwed
1.Create a exception class 
2. Create a Exception policy to log DL layer exception.
3. Create 2 store procs to write log into db
4. the code ta would call the logging policy.
Can any one YELL me what is that i am missing.....


using

 

System;

 

using

 

System.Collections.Generic;

 

using

 

System.Text;

 

#region

 

EnterpriseLibrary

 

using

 

Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging;

 

using

 

Microsoft.Practices.EnterpriseLibrary.ExceptionHandling;

 

using

 

Microsoft.Practices.EnterpriseLibrary.Logging;

 

#endregion

 

EnterpriseLibrary

 

namespace

 

DALLayer

 

{

 

public class DalExceptionClass : System.Exception

 

{

 

/// <summary>

 

 

/// Exception manager for DataAccess Layer

 

 

/// </summary>

 

 

private System.String ErrorCodeField;

 

 

public System.String ErrorCode

 

{

 

get { return ErrorCodeField; }

 

 

set { ErrorCodeField = value; }

 

}

 

private System.String ErrorMessageField;

 

 

public System.String ErrorMessage

 

{

 

get { return ErrorMessageField; }

 

 

set { ErrorMessageField = value; }

 

}

 

public DalExceptionClass(String errorCode, String errorMessage)

 

{

 

this.ErrorCodeField = errorCode;

 

 

this.ErrorMessageField = errorMessage;

 

}

 

public void LogException(Exception ex)

 

{

 

try

 

{

 

ExceptionPolicy.HandleException(ex, "DALLayerExceptionPolicy");

 

}

 

catch (Exception logex)

 

{

 

LogEntry logEntry = new LogEntry();

 

logEntry.Message = logex.Message;

 

Logger.Write(logEntry);

 

}

}

 

public void LogDALException(DalExceptionClass dlex)

 

{

 

Exception ex = new Exception(dlex.ErrorMessage);

 

 

try

 

{

 

LogEntry logEntry = new LogEntry();

 

logEntry.Message = dlex.Message;

 

Logger.Write(logEntry);

 

 

ExceptionPolicy.HandleException(ex, ConfigKeyConstants.DATA_ACCESS_EXCEPTION_POLICY);

 

}

 

catch (Exception logex)

 

{

 

LogEntry logEntry = new LogEntry();

 

logEntry.Message = logex.Message;

 

Logger.Write(logEntry);

 

}

}

}

}

 

<?

 

xml version="1.0"?>

 

<!--

 

 

Note: As an alternative to hand editing this file you can use the

web admin tool to configure settings for your application. Use

the Website->Asp.Net Configuration option in Visual Studio.

A full list of settings and comments can be found in

machine.config.comments usually located in

\Windows\Microsoft.Net\Framework\v2.x\Config

 

 

 

 

-->

<

 

configuration>

 

<

 

configSections>

 

<

 

section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />

 

<

 

section name="exceptionHandling" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Configuration.ExceptionHandlingSettings, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />

 

<

 

section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />

 

</

 

configSections>

 

<

 

loggingConfiguration name="Logging Application Block" tracingEnabled="true"

 

 

 

 

 

 

defaultCategory="General" logWarningsWhenNoCategoriesMatch="true">

 

<

 

listeners>

 

<

 

add databaseInstanceName="LocalSqlServer" writeLogStoredProcName="sp_WriteLog"

 

 

 

 

 

 

addCategoryStoredProcName="sp_AddCategory" formatter="Text Formatter"

 

 

 

 

 

 

listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Database.Configuration.FormattedDatabaseTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging.Database, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"

 

 

 

 

 

 

traceOutputOptions="None" type="Microsoft.Practices.EnterpriseLibrary.Logging.Database.FormattedDatabaseTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging.Database, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"

 

 

 

 

 

 

name="Database Trace Listener" />

 

</

 

listeners>

 

<

 

formatters>

 

<

 

add template="Timestamp: {timestamp}&#xD;&#xA;Message: {message}&#xD;&#xA;Category: {category}&#xD;&#xA;Priority: {priority}&#xD;&#xA;EventId: {eventid}&#xD;&#xA;Severity: {severity}&#xD;&#xA;Title:{title}&#xD;&#xA;Machine: {machine}&#xD;&#xA;Application Domain: {appDomain}&#xD;&#xA;Process Id: {processId}&#xD;&#xA;Process Name: {processName}&#xD;&#xA;Win32 Thread Id: {win32ThreadId}&#xD;&#xA;Thread Name: {threadName}&#xD;&#xA;Extended Properties: {dictionary({key} - {value}&#xD;&#xA;)}"

 

 

 

 

 

 

type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"

 

 

 

 

 

 

name="Text Formatter" />

 

</

 

formatters>

 

<

 

categorySources>

 

<

 

add switchValue="All" name="General">

 

<

 

listeners>

 

<

 

add name="Database Trace Listener" />

 

</

 

listeners>

 

</

 

add>

 

</

 

categorySources>

 

<

 

specialSources>

 

<

 

allEvents switchValue="All" name="All Events" />

 

<

 

notProcessed switchValue="All" name="Unprocessed Category" />

 

<

 

errors switchValue="All" name="Logging Errors &amp; Warnings">

 

<

 

listeners>

 

<

 

add name="Database Trace Listener" />

 

</

 

listeners>

 

</

 

errors>

 

</

 

specialSources>

 

</

 

loggingConfiguration>

 

<

 

exceptionHandling>

 

<

 

exceptionPolicies>

 

<

 

add name="DALLayerExceptionPolicy">

 

<

 

exceptionTypes>

 

<

 

add type="System.Exception, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"

 

 

 

 

 

 

postHandlingAction="None" name="Exception">

 

<

 

exceptionHandlers>

 

<

 

add logCategory="General" eventId="100" severity="Error" title="DAL"

 

 

 

 

 

 

formatterType="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.TextExceptionFormatter, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"

 

 

 

 

 

 

priority="4" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging.LoggingExceptionHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"

 

 

 

 

 

 

name="Logging Handler" />

 

</

 

exceptionHandlers>

 

</

 

add>

 

</

 

exceptionTypes>

 

</

 

add>

 

<

 

add name="UIExceptionPolicy">

 

<

 

exceptionTypes>

 

<

 

add type="System.Exception, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"

 

 

 

 

 

 

postHandlingAction="None" name="Exception">

 

<

 

exceptionHandlers>

 

<

 

add logCategory="General" eventId="103" severity="Error" title="UI"

 

 

 

 

 

 

formatterType="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.TextExceptionFormatter, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"

 

 

 

 

 

 

priority="5" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging.LoggingExceptionHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"

 

 

 

 

 

 

name="Logging Handler" />

 

</

 

exceptionHandlers>

 

</

 

add>

 

</

 

exceptionTypes>

 

</

 

add>

 

</

 

exceptionPolicies>

 

</

 

exceptionHandling>

 

<

 

appSettings />

 

<

 

system.web>

 

<!--

 

 

Set compilation debug="true" to insert debugging

symbols into the compiled page. Because this

affects performance, set this value to true only

during development.

 

 

-->

 

<

 

compilation debug="true">

 

<

 

assemblies>

 

<

 

add assembly="System.Management, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A" />

 

<

 

add assembly="System.Messaging, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A" />

 

<

 

add assembly="System.Configuration.Install, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A" />

 

<

 

add assembly="System.Data.OracleClient, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" />

 

<

 

add assembly="System.Transactions, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" />

 

</

 

assemblies>

 

</

 

compilation>

 

<!--

 

 

 

 

The <authentication> section enables configuration

of the security authentication mode used by

ASP.NET to identify an incoming user.

 

 

-->

 

<

 

authentication mode="Windows" />

 

<!--

 

 

 

 

The <customErrors> section enables configuration

of what to do if/when an unhandled error occurs

during the execution of a request. Specifically,

it enables developers to configure html error pages

to be displayed in place of a error stack trace.

<customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">

<error statusCode="403" redirect="NoAccess.htm" />

<error statusCode="404" redirect="FileNotFound.htm" />

</customErrors>

 

 

-->

 

</

 

system.web>

 

</

 

configuration>

 

 

 

 

 

set

 

ANSI_NULLS ON

 

set

 

QUOTED_IDENTIFIER ON

 

 

 

 

 

GO

 

 

 

ALTER

 

PROCEDURE [dbo].[sp_WriteLog]

 

 

 

 

(

 

 

 

 

@EventID

INT,

 

@Priority

INT,

 

@Severity

NVARCHAR(32),

 

@Title

NVARCHAR(256),

 

@Timestamp

DATETIME,

 

 

 

 

 

@MachineName

NVARCHAR(32),

 

@AppDomainName

NVARCHAR(512),

 

 

 

 

 

@ProcessID

NVARCHAR(256),

 

 

 

 

 

@ProcessName

NVARCHAR(512),

 

 

 

 

 

@ThreadName

NVARCHAR(512),

 

 

 

 

 

@Win32ThreadId

NVARCHAR(128),

 

 

 

 

 

@Message

NVARCHAR(1500),

 

 

 

 

 

@FormattedMessage

NTEXT,

 

 

 

 

 

@LogId

INT OUTPUT

 

 

 

 

 

)

 

 

 

 

AS

 

 

 

 

 

 

-- TRY block starts --

 

 

 

 

BEGIN

 

TRY

 

 

 

 

 

 

INSERT INTO [Log] (

 

 

 

 

 

EventID

,

 

 

 

 

 

Priority

,

 

 

 

 

 

Severity

,

 

 

 

 

 

Title

,

 

 

 

 

 

[Timestamp]

,

 

 

 

 

 

MachineName

,

 

 

 

 

 

AppDomainName

,

 

 

 

 

 

ProcessID

,

 

 

 

 

 

ProcessName

,

 

 

 

 

 

ThreadName

,

 

 

 

 

 

Win32ThreadId

,

 

 

 

 

 

 

Message,

 

 

 

 

 

FormattedMessage

 

)

 

 

 

 

 

 

VALUES (

 

 

 

 

 

@EventID

,

 

@Priority

,

 

@Severity

,

 

@Title

,

 

@Timestamp

,

 

 

 

 

 

@MachineName

,

 

@AppDomainName

,

 

 

 

 

 

@ProcessID

,

 

 

 

 

 

@ProcessName

,

 

 

 

 

 

@ThreadName

,

 

 

 

 

 

@Win32ThreadId

,

 

 

 

 

 

@Message

,

 

 

 

 

 

@FormattedMessage

)

 

 

 

 

 

 

SET @LogID = @@IDENTITY

 

 

 

 

 

 

 

RETURN @LogID

 

 

 

 

END

 

TRY

 

 

 

 

 

-- TRY block ends --

-- Catch block starts --

 

 

 

 

BEGIN

 

CATCH

 

 

 

 

 

 

-- If there was an error, raise an error with the details of the exception

 

 

 

 

 

 

DECLARE @ErrMsg nvarchar(4000),

 

@ErrSeverity

int

 

 

SELECT @ErrSeverity = ERROR_SEVERITY(),

 

 

 

 

 

@ErrMsg

= ' @ErrMsg:' + ERROR_MESSAGE() +

 

 

 

 

 

 

' @ErrorNo:' + CONVERT(NVARCHAR,ERROR_NUMBER()) +

 

 

 

 

 

 

' @ErrorState:' + CONVERT(NVARCHAR,ERROR_STATE()) +

 

 

 

 

 

 

' @ErrSeverity:' + CONVERT(NVARCHAR,@ErrSeverity) +

 

 

' @Sp_Name:' + 'sp_WriteLog'

 

 

 

 

 

 

 

RAISERROR(@ErrMsg, @ErrSeverity, 1)

 

 

 

 

 

END

 

CATCH

 

 

 

 

 

-- Catch block ends --

 

using

 

System;

 

 

 

 

using

 

System.Collections.Generic;

 

 

 

 

using

 

System.Text;

 

 

 

 

using

 

Microsoft.Practices.EnterpriseLibrary.Data;

 

 

 

 

using

 

System.Data.SqlClient;

 

 

 

 

namespace

 

DALLayer

 

{

 

public class Controller

 

 

 

 

 

{

 

private string strErrorCode = "101";

 

 

private string strErrorMessage = "Hello";

 

 

DalExceptionClass dlex;

 

 

public void ExceptionTryDal()

 

{

 

try

 

 

 

 

 

{

 

string hello = string.Empty;

 

 

int asdf = 12;

 

 

int wer = 0;

 

asdf = asdf / wer;

}

 

catch (SqlException sqex)

 

{

strErrorCode = sqex.ErrorCode.ToString();

strErrorMessage = sqex.Message.ToString();

dlex =

new DalExceptionClass(strErrorCode, strErrorMessage);

 

dlex.LogDALException(dlex);

 

throw dlex;

 

}

 

catch (Exception ex)

 

{

dlex =

new DalExceptionClass(strErrorCode, ex.Message);

 

dlex.LogException(ex);

 

throw dlex;

 

}

}

}

}

 

using

 

System;

 

 

 

 

using

 

System.Collections.Generic;

 

 

 

 

using

 

System.Text;

 

 

 

 

using

 

Microsoft.Practices.EnterpriseLibrary.Data;

 

 

 

 

using

 

System.Data.SqlClient;

 

 

 

 

namespace

 

DALLayer

 

{

 

public class Controller

 

 

 

 

 

{

 

private string strErrorCode = "101";

 

 

private string strErrorMessage = "Hello";

 

 

DalExceptionClass dlex;

 

 

public void ExceptionTryDal()

 

{

 

try

 

 

 

 

 

{

 

string hello = string.Empty;

 

 

int asdf = 12;

 

 

int wer = 0;

 

asdf = asdf / wer;

}

 

catch (SqlException sqex)

 

{

strErrorCode = sqex.ErrorCode.ToString();

strErrorMessage = sqex.Message.ToString();

dlex =

new DalExceptionClass(strErrorCode, strErrorMessage);

 

dlex.LogDALException(dlex);

 

throw dlex;

 

}

 

catch (Exception ex)

 

{

dlex =

new DalExceptionClass(strErrorCode, ex.Message);

 

dlex.LogException(ex);

 

throw dlex;

 

}

}

}

}

Dec 7, 2008 at 1:47 PM
Hi,

Have you encountered any exception when running your sample code? If not, I suggest that you run a SQL Profiler against your sample app to check if your Sprocs are executed. Also if you can send your solution, that would be greatly appreciated. Thanks.


Valiant Dudan
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com
Dec 7, 2008 at 4:41 PM
thanks for the response.... But i got the fix...It was not connecting to my database....\
Connection string was missing...
Sowie for late resposne... have  a great day