I am working on a project that wants to implement a logging utilizing the Microsoft Enterprise Library 5.0. The project has a need to create different types of logs (Auditing, Exception, …). This log information is going to be stored in different tables in a SQL Server database. Each table will have its own schema, capturing different data points required for the type of log. The rest of the application is using Entity Framework so I thought I would utilize EF for the logging.
Log Entry to the Rescue
The first step is to come up with the schema that you want to use to store your log information. Here is the entity of that I am going to use in the example for this post:
CustomLogEntry log = new CustomLogEntry(); log.Message = txtLog.Text; log.Categories.Add(Category.General); log.Priority = Priority.Normal; log.MyStuff = string.Format("This is my stuff {0}", txtLog.Text);
I am capturing the data that is part of the LogEntry object plus I added my own column called MyStuff. This could have been arbitrary data but LogEntry contains the data I want to capture. Plus you can use the Severity to filter what gets log.
Since I want most of the data from the LogEntry object I created my own entry object that inherits from LogEntry:
By inheriting from LogEntry I get all the goodness of the default logging framework and the ability to extend it to my will.
Who is Listening
Now that I have my LogEntry object I have to create my trace listener to write it out to the database. The logging block use trace listeners to direct the entries to the appropriate destination. You set up logging categories that can have 1 to many trace listeners. The category allows you to filter out what gets sent to the listener based on the severity. Next comes the listener. My listener inherits from CustomTraceListener. Which is pretty basic. There are three methods that you need to override TraceData, Write, and WriteLine. In the TraceData method you determine if data coming in is of type CustomLogEntry. if it is you pass it to the write method that implements the call to the Entity Framework. Here is my custom listener:
Here I am mapping my CustomLogEntry to my entity. Optionally I could use a mapper or create a POCO Entity that inherits for LogEntry by modifying the T4 template. I should also abstract out the data call to a repository so that I could easily switch out the implementation. I did not do these things because this was a prototype to see how easily I can log data to a custom schema.
Configuration Time
Ok the pieces are now in place, what do I do. The Enterprise Library come with a great tool for setting up the configuration. Right click on the configuration file and select “Edit Enterprise Library V5 Configuration”:
Here you can wire up your listener to which ever category you want. Here my listener is wire to the General Category
I won’t get into how to get your custom listener to display here but the Hands on Labs do a good job explaining this.
Log That Message
Now all you have to do is log messages. Here is a code snippet that does just that:
CustomLogEntry log = new CustomLogEntry(); log.Message = txtLog.Text; log.Categories.Add(Category.General); log.Priority = Priority.Normal; log.MyStuff = string.Format("This is my stuff {0}", txtLog.Text);
Summary
That is all there is to it. There are other improvements that could be made but for the most part this should show you the control that you have when it comes to where you send your log information.
The source code for this example can be downloaded here.
Leave a Reply