Enterprise Library v2.0 : log to database
How to setup logging to a database with EntLib v2.0?
- Create logging database with tables and stored procedures
Script can be found in [Install Folder]\Microsoft Enterprise Library January 2006\src\Logging\TraceListeners\Database\Scripts. - Create new configuration file with the Enterprise Library Configuration Tool.
- Add Data Access Application Block and set connectionString to logging database.
- Add Logging Application Block and add a Database Trace Listener to the Trace Listeners.
- Set DatabaseInstanceName and formatter on created Database Trace Listener.
- Add reference to the created Database Trace Listener in the general category.
- Add references Microsoft.Practices.EnterpriseLibrary.Logging and Microsoft.Practices.EnterpriseLibrary.Logging.Database to your project.
- Write some code to log to the database.
try
{
throw new Exception("My Special Exception");
}
catch (Exception ex)
{
//Logging
LogEntry logEntry = new LogEntry();
logEntry.Message = ex.ToString();
logEntry.Title = ex.Message;
logEntry.Priority = 1;
logEntry.Severity = System.Diagnostics.TraceEventType.Error;
Logger.Write(logEntry);
}
Don't forget the reference in your project to Microsoft.Practices.EnterpriseLibrary.Logging.Database. Otherwise you will end up with the Invalid TraceListenerData type in configuration error.
16 Comments:
At 11:00 AM, Anonymous said…
This article is fine to understand, I tried like this but am getting exception like "The value can not be null or string or empty. Parameter name: name". Where is the mistake? Please help me.
At 11:12 AM, Pieter said…
Be sure to set the formatter in the database trace listener. I just noticed that it's not set in the screenshot I published ...
At 6:47 AM, Michael Freidgeim said…
To log exception you can use Exception handling block http://www.devx.com/dotnet/Article/31463
At 4:50 PM, Anonymous said…
To the first commentor:
You haven't selected a formatter in the Database Trace Listener. Once you set that it should work just fine.
Newms
At 12:25 PM, Anonymous said…
Thank you. no other article mentioned Add reference to the created Database Trace Listener in the general category. its easy to miss out.
At 2:58 PM, Anonymous said…
Can I use it in order to log into Oracle db?
At 3:17 PM, Pieter said…
I haven't tried it, but it certainly should be possible. That's the whole idea of the Enterprise Library!
At 1:04 PM, G-Force said…
it is working with Sql server but i cant log into oracle db..plz solve my problems.. i create scripts and tables as said sqlserver...
At 1:16 PM, G-Force said…
i tried with oracle db but i cant log..i have created scripts and tables for oracle
plz help me to sovle the problem
no error..
At 2:16 PM, Unknown said…
Hi, I am using Enterprise library . I am using Logging Application Blog with DataBaseTraceListener. I have configure connection string, listener etc. But when I tried to Write LogEntry using Logger it gives error as "Message = {The value can not be null or string or empty.
Parameter name: name} Source = { Microsoft.Practices.EnterpriseLibrary.Common } " I tried this with FlatFileTraceListener, it is working. Also I have given all values to LogEntry object. But it is giving me same error for database.
At 1:19 AM, Anonymous said…
Thanks for you post and that little comment at the end, i.e. Don't forget the reference in your project to Microsoft.Practices.EnterpriseLibrary.Logging.Database. Otherwise you will end up with the Invalid TraceListenerData type in configuration error.
You saved me hours of time in tring to figure out that error
At 12:44 PM, Unknown said…
What formatter should I use (text, binary or custom)? I've a problem that no log record were inserted.
At 12:46 PM, Unknown said…
What formatter should I use (text, binary or custom)? I've a problem of no log record were inserted.
At 2:58 AM, Unknown said…
I just want to use one DB table and my own stored proc to enter information in the table to store exception information. I am not receiving any error but there is no entry in the table also. Any ideas?
At 10:25 PM, Anonymous said…
Even though there is no error in your application, there could be error in Enterprise Library database logging process. Check Windows Event log (or whatever is your default error listener) for error related to Data logging.
At 1:17 AM, m0nkey said…
If logging to the database fails, how do you ensure the error gets written to a flatfile?
Post a Comment
<< Home