[This is preliminary documentation and is subject to change.]

Writes log messages to the database using an ADO.NET provider.

Namespace:  NLog.Targets
Assembly:  NLog (in NLog.dll) Version: 2.0.1.0

Syntax

C#
public sealed class DatabaseTarget : Target, 
	IInstallable
Visual Basic (Declaration)
Public NotInheritable Class DatabaseTarget _
	Inherits Target _
	Implements IInstallable
Visual Basic (Usage)
Dim instance As DatabaseTarget

Examples

The configuration is dependent on the database type, because there are differnet methods of specifying connection string, SQL command and command parameters.

MS SQL Server using System.Data.SqlClient:

CopyXML
 1<?xml version="1.0" ?>
 2<nlog autoReload="true">
 3    <targets>
 4        <target name="database" type="Database">
 5
 6            <dbprovider>mssql</dbprovider>
 7
 8            <!-- database connection parameters -->
 9            <!-- alternatively you could provide a single 'connectionstring' parameter -->
10
11            <dbhost>.</dbhost> 
12            <dbdatabase>NLogDatabase</dbdatabase>
13            <dbusername>nloguser</dbusername>
14            <dbpassword>nlogpassword</dbpassword>
15
16            <commandText>
17                insert into LogTable(time_stamp,level,logger,message) values(@time_stamp, @level, @logger, @message);
18            </commandText>
19
20            <parameter name="@time_stamp" layout="${date}" />
21            <parameter name="@level" layout="${level}" />
22            <parameter name="@logger" layout="${logger}" />
23            <parameter name="@message" layout="${message}" />
24        </target>
25    </targets>
26
27    <rules>
28        <logger name="*" minlevel="Debug" appendTo="database" />
29    </rules>
30</nlog>

Oracle using System.Data.OracleClient:

CopyXML
 1<?xml version="1.0" ?>
 2<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
 3      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
 4
 5    <!-- configuration contributed by David Maly -->
 6
 7    <targets>
 8        <target name="database" xsi:type="Database" keepConnection="false" useTransactions="true"
 9                dbProvider="System.Data.OracleClient.OracleConnection,System.Data.OracleClient, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
10                connectionString="Data Source=MYORACLEDB;User Id=DBO;Password=MYPASSWORD;Integrated Security=no;"
11                commandText="insert into LOGTABLE( TIME_STAMP,LOGLEVEL,LOGGER,CALLSITE,MESSAGE) values( :TIME_STAMP,:LOGLEVEL,:LOGGER,:CALLSITE,:MESSAGE)">
12            <parameter name="TIME_STAMP" layout="${longdate}" />
13            <parameter name="LOGLEVEL" layout="${level:uppercase=true}" />
14            <parameter name="LOGGER" layout="${logger}" />
15            <parameter name="CALLSITE" layout="${callsite:filename=true}" />
16            <parameter name="MESSAGE" layout="${message}" />
17        </target>
18    </targets>
19
20    <rules>
21        <logger name="*" minlevel="Debug" writeTo="database" />
22    </rules>
23</nlog>

Oracle using System.Data.OleDBClient:

CopyXML
 1<?xml version="1.0" ?>
 2<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
 3      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
 4
 5    <!-- configuration contributed by David Maly -->
 6
 7    <targets>
 8        <target name="database" xsi:type="Database" keepConnection="false" useTransactions="true" dbProvider="oledb"
 9            connectionString="Provider=msdaora;Data Source=MYORACLEDB;User Id=DBO;Password=MYPASSWORD;"
10            commandText="insert into LOGTABLE( TIME_STAMP,LOGLEVEL,LOGGER,CALLSITE,MESSAGE) values(?,?,?,?,?)">
11            <parameter name="TIME_STAMP" layout="${longdate}" />
12            <parameter name="LOGLEVEL" layout="${level:uppercase=true}" />
13            <parameter name="LOGGER" layout="${logger}" />
14            <parameter name="CALLSITE" layout="${callsite:filename=true}" />
15            <parameter name="MESSAGE" layout="${message}" />
16        </target>
17    </targets>
18
19    <rules>
20        <logger name="*" minlevel="Debug" writeTo="database" />
21    </rules>
22</nlog>

To set up the log target programmatically use code like this (an equivalent of MSSQL configuration):

CopyC#
 1using NLog;
 2using NLog.Targets;
 3
 4class Example
 5{
 6    static void Main(string[] args)
 7    {
 8        DatabaseTarget target = new DatabaseTarget();
 9        DatabaseParameterInfo param;
10
11        target.DBProvider = "mssql";
12        target.DBHost = ".";
13        target.DBUserName = "nloguser";
14        target.DBPassword = "pass";
15        target.DBDatabase = "databasename";
16        target.CommandText = "insert into LogTable(time_stamp,level,logger,message) values(@time_stamp, @level, @logger, @message);";
17
18        param = new DatabaseParameterInfo();
19        param.Name = "@time_stamp";
20        param.Layout = "${date}";
21        target.Parameters.Add(param);
22
23        param = new DatabaseParameterInfo();
24        param.Name = "@level";
25        param.Layout = "${level}";
26        target.Parameters.Add(param);
27
28        param = new DatabaseParameterInfo();
29        param.Name = "@logger";
30        param.Layout = "${logger}";
31        target.Parameters.Add(param);
32
33        param = new DatabaseParameterInfo();
34        param.Name = "@message";
35        param.Layout = "${message}";
36        target.Parameters.Add(param);
37
38        NLog.Config.SimpleConfigurator.ConfigureForTargetLogging(target, LogLevel.Debug);
39
40        Logger logger = LogManager.GetLogger("Example");
41        logger.Debug("log message");
42    }
43}

Inheritance Hierarchy

Object
  NLog.Targets..::.Target
    NLog.Targets..::.DatabaseTarget

See Also