How Logging in to Custom database?

Topics: Logging Application Block
Apr 20, 2009 at 1:51 AM

I am using Enterprise library version 4.1. I am trying to write the logs in to database

I have created the database.

CREATE

 TABLE [dbo].[Log](

 [LogId] [int] IDENTITY(1,1) NOT NULL,

 [LogTime] [datetime] NULL,

 [Severity] [varchar](25) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,

 [Userid] [varchar](25) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,

 [ClassName] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,

 [FunctionName] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,

 [Message] [varchar](512) COLLATE SQL_Latin1_General_CP1_CI_AS NULL

 )

ON [PRIMARY]

can you give me some example how to log custome log message into database?

please help me out

Thanks

Apr 20, 2009 at 2:09 AM
You would need to create a custom database tracelistener for your custom database.  Please refer to the documentation on how to create custom tracelistener.  You can copy the logic of the methods from that of the FormattedDatabaseTracelistener of the entlib source code.


Sarah Urmeneta
Global Technology & Solutions
Avanade, Inc.
entlib.support@avanade.com
Apr 20, 2009 at 2:46 AM

namespace Common
{
    [ConfigurationElementType(typeof(FormattedDatabaseTraceListenerData))]
    public class CustomDatabaseTraceListener : FormattedTraceListenerBase
    {
        private Database data = DatabaseFactory.CreateDatabase();

        public override void Write(string message)
        {
            ExecuteWriteLogStoredProcedure(DateTime.Now, string.Empty,string.Empty, string.Empty, string.Empty, message);
        }

        public override void WriteLine(string message)
        {
            Write(message);
        }

        private void ExecuteWriteLogStoredProcedure(DateTime logId, string severity, string userId,string className,string functionName,string message)
        {
            DbCommand storedProcCommand = data.GetStoredProcCommand("WriteLog");
            data.AddInParameter(storedProcCommand, "LogID", DbType.DateTime, logId);
            data.AddInParameter(storedProcCommand, "Severity", DbType.String, severity);
            data.AddInParameter(storedProcCommand, "UserID", DbType.String, userId);
            data.AddInParameter(storedProcCommand, "className", DbType.String, className);
            data.AddInParameter(storedProcCommand, "FunctionName", DbType.String, functionName);
            data.AddInParameter(storedProcCommand, "Message", DbType.String, message);
            data.ExecuteNonQuery(storedProcCommand);
      
        }

    }
}

Apr 20, 2009 at 3:05 AM
I don't think you can specify this as a custom trace listener in the config using the entlib configuration tool.  
[ConfigurationElementType(typeof(FormattedDatabaseTraceListenerData))] - should be  [ConfigurationElementType(typeof(CustomTraceListenerData))].  You should also inherit from CustomTraceListener.

Do you have any questions regarding this?  Not sure if you're asking something when you posted the code here.


Sarah Urmeneta
Global Technology & Solutions
Avanade, Inc.
entlib.support@avanade.com