This is a simple MVC sample which shows how to set up a basic Enterprise Library Semantic Logging (SLAB) application which logs to the database.
Create a simple MVC project
Get the Semantic Logging from nuget:
These nuget packages adds just 4 new assemblies as shown (No Unity):
Install the scripts to your database:
Scripts can be found here ..\packages\EnterpriseLibrary.SemanticLogging.Database.1.0.1304.0\scripts
Step 1 Create an EventSource
using System.Diagnostics.Tracing; namespace MvcEL6Test.Logger { [EventSource(Name = "BasicLogger")] public class BasicLogger : EventSource { public static readonly BasicLogger Log = new BasicLogger(); [Event(1, Message = "{0}", Level = EventLevel.Critical)] public void Critical(string message) { if (IsEnabled()) WriteEvent(1, message); } [Event(2, Message = "{0}", Level = EventLevel.Error)] public void Error(string message) { if (IsEnabled()) WriteEvent(2, message); } [Event(3, Message = "{0}", Level = EventLevel.Warning)] public void Warning(string message) { if (IsEnabled()) WriteEvent(3, message); } } }
Step 2 Use the logger in code
using System.Web.Mvc; using MvcEL6Test.Logger; namespace MvcEL6Test.Controllers { public class HomeController : Controller { // // GET: /Home/ public ActionResult Index() { BasicLogger.Log.Critical("Hello World Critical"); BasicLogger.Log.Error("Hello World Error"); BasicLogger.Log.Warning("Hello World Warning"); return View(); } } }
Step 3 Consume the logger or add a sink
In Global.asax.cs
using System.Diagnostics.Tracing; using System.Web.Http; using System.Web.Mvc; using System.Web.Optimization; using System.Web.Routing; using Microsoft.Practices.EnterpriseLibrary.SemanticLogging; using MvcEL6Test.Logger; namespace MvcEL6Test { // Note: For instructions on enabling IIS6 or IIS7 classic mode, // visit http://go.microsoft.com/?LinkId=9394801 public class MvcApplication : System.Web.HttpApplication { protected void Application_Start() { AreaRegistration.RegisterAllAreas(); WebApiConfig.Register(GlobalConfiguration.Configuration); FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRoutes(RouteTable.Routes); BundleConfig.RegisterBundles(BundleTable.Bundles); var sqlListener = SqlDatabaseLog.CreateListener("HomeController", "Data Source=.;Initial Catalog=Logging;Integrated Security=True"); sqlListener.EnableEvents(BasicLogger.Log, EventLevel.Error); } } }
Now your finished, your logs can be viewed in the database:
Note: Our warning log was not logged due the the sink configuration in the global.asax
Source code:
https://github.com/damienbod/MVCEL6DbLogging.git
Enterprise Library 6, Semantic Logging, Part 2, OUT-OF-PROCESS
Enterprise Library 6, Semantic Logging, Part 3, Getting into the details
Enterprise Library 6, Semantic Logging, Part 4 advantages, customising
Links:
http://www.codeproject.com/Articles/602077/InstrumentationpluswithplusSemanticplusLoggingplus
http://entlib.codeplex.com/wikipage?title=Entlib6CTPReleaseNotes
http://blogs.msdn.com/b/agile/archive/2013/02/07/embracing-semantic-logging.aspx
http://channel9.msdn.com/posts/Introducing-Semantic-Logging
Thanks for the source code it was a very helpful sanity check for me. I’m trying to integrate SLAB with a few WCF services and I cannot get the SqlDatabaseLog Listener to write events to the db. I was able to use your MVC app and write events to my DB. Do you know how I could configure the listener in process with a WCF project? I can’t do out-of-process b/c it seems like that is still CTP.
Hi Arnie, thanks for your comment. You have to add the SemanticLogging to your project and you have to define and also enable your sqlListener for each EventSource class.
var sqlListener = SqlDatabaseLog.CreateListener(“HomeController”, “Data Source=.;Initial Catalog=Logging;Integrated Security=True”);
sqlListener.EnableEvents(BasicLogger.Log, EventLevel.Warning);
Hope this helps greetings Damien
Thanks, Damien, but I’m trying to figure out where can you enable the listener, since there is no application_start?
Does the application have no static void Main(string[] args)?, What type of application have you? You could also just use a singleton class which inits your listener in your wcf constructor class. greetings Damien
It’s a WCF application w/ 2 projects. 1 class library and 1 as a service host. I initialized the listener in the constructor of the library and it works. Not sure if that’s the best place to put it, but I’ll keep it there for now. Thanks for your help.
[…] the example code from here I added a BasicLogger […]
[…] the example code from here I added a BasicLogger […]
[…] the example code from here I added a BasicLogger […]
Reblogged this on Happy DotNetting.