Skip to content
ladenedge edited this page Nov 1, 2012 · 5 revisions

NLog.EntityFramework library provides a database target for NLog that is configurable via an Entity Framework connection string.

Installation

NLog.EntityFramework is available via NuGet. To install it, run the following command in the Package Manager Console:

PM> Install-Package NLog.EntityFramework

To register the library as an NLog extension in your application's configuration file, include the following code in your <nlog> element:

<extensions>
  <add assembly="NLog.EntityFramework"/>
</extensions>

Usage

This library provides a single NLog target. It is nearly identical to the existing Database target, except that the connectionStringName attribute must refer to an Entity Framework-style connection string.

A new target using this extension might look something like this:

<target name="name"
        xsi:type="EntityFramework"
        connectionStringName="Entities" ...

Example

The following is a fairly complete example of what you might see in a finished app.config:

<configuration>
  <connectionStrings>
    <add name="Entities" connectionString="metadata=..." providerName="System.Data.EntityClient" />
  </connectionStrings>
<nlog>
  <extensions>
    <add assembly="NLog.EntityFramework"/>
  </extensions>
  <targets>
    <target name="db"
            xsi:type="EntityFramework"
            connectionStringName="Entities"
            commandText="INSERT INTO ...">
      <parameter name="@thread" layout="[${threadid}] ${threadname}" />
      <parameter name="@log_level" layout="${level}" />
      <parameter name="@logger" layout="${logger}" />
      <parameter name="@message" layout="${message}" />
      <parameter name="@exception" layout="${exception:format=ToString}" />
    </target>
  </targets>
  <rules>
    <logger name="*" minlevel="Trace" writeTo="db" />
  </rules>
</nlog>
Clone this wiki locally