This blog demonstrates how to log to the database using semantic logging (SLAB) OUT-OF-PROCESS in a MVC4 application.
Follow the instructions as shown in part 1
Enterprise Library 6, Semantic Logging, Part 1, database listener
UPDATE: The service is now available as a NuGet package.
Download and install the OUT-OF-PROCESS service:
http://go.microsoft.com/fwlink/p/?LinkID=290903
Follow the instructions shown here:
http://entlib.codeplex.com/wikipage?title=Entlib6CTPReleaseNotes
Taken from the docs:
To use the out-of-proc SLAB service, follow these steps: Create an event source class that defines your log messages by extending the EventSource class in the System.Diagnostics.Tracing namespace. Add code to your application that generates log messages by invoking methods in your EventSource implementation. If you want to collect and process log messages out-of-process, download and install the SemanticLogging-svc.exe Windows Service and edit the supplied configuration file (SemanticLogging-svc.xml) to define the event listeners. The block includes an XML schema file (SemanticLogging-svc.xsd) to support editing this configuration file in Visual Studio. The download includes a PowerShell script, install-packages.ps1, that uses NuGet to download and install the dependencies of SemanticLogging-svc.exe in the current folder. To install and run SemanticLogging-svc.exe as a Windows service, run SemanticLogging-svc.exe -start. To install and run SemanticLogging-svc.exe as a console application, run SemanticLogging-svc.exe -console. (Optionally) If you are using one of the database event listeners, run the supplied script to create a suitable database to store log messages.
NOTE: you can run your service as a console application or as a service.
The service is configured now to log BasicLogger events to the log file SemanticLogging-svc.runtime.log. Now we log out-of-process to the file and in process to the database.
Now we can log out of process to the database:
<sqlDatabaseSink name="myDb" instanceName="OoPHomeController" connectionString="Data Source=.;Initial Catalog=Logging;Integrated Security=True"> <sources> <!-- The below settings shows a simple configuration sample for the buit-in non-transient fault tracing --> <!-- Remove this eventSource if you'd like, and add your own configuration according to the documentation --> <!-- The name attribute is from the EventSource.Name Property --> <eventSource name="BasicLogger" level="LogAlways"/> </sources> </sqlDatabaseSink>
And here is the schema for the sql listener configuration.
<xs:complexType name="SqlDatabaseSinkElement"> <xs:complexContent mixed="false"> <xs:extension base="SinkBase"> <xs:attribute name="instanceName" type="NoEmptyString" use="required" /> <xs:attribute name="connectionString" type="NoEmptyString" use="required" /> <xs:attribute name="tableName" type="NoEmptyString" use="optional" /> <xs:attribute name="bufferingIntervalInSeconds" type="PositiveAndMinusOneInteger" use="optional" /> <xs:attribute name="bufferingCount" type="PositiveInteger" use="optional" /> <xs:attribute name="maxBufferSize" type="PositiveInteger" use="optional" /> <xs:attribute name="bufferingFlushAllTimeoutInSeconds" type="PositiveAndMinusOneInteger" use="optional" /> </xs:extension> </xs:complexContent> </xs:complexType>
Source code:
https://github.com/damienbod/MVCEL6DbLogging.git
Enterprise Library 6, Semantic Logging, Part 1, database listener
Enterprise Library 6, Semantic Logging, Part 3, Getting into the details
Enterprise Library 6, Semantic Logging, Part 4 advantages, customising
Reblogged this on Happy DotNetting.