-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Installing targets
Some targets support installing. Current the following targets support installing
- Database target
- EventLogger target
- PerformanceCounter target
Install all targets used in the config:
LogManager.Configuration.Install(new InstallationContext());
Uninstall all targets used in the config:
LogManager.Configuration.Uninstall(new InstallationContext());
<target xsi:type="Database" name="db"
connectionStringName="LoggingDatabase">
<install-command>
<text>
<!--
NOTE: call LogManager.Configuration.Install(new InstallationContext());
to execute this query.
-->
CREATE TABLE ${var:logTableName} (
Id bigint primary key not null identity(1,1),
Logged datetime2,
Level nvarchar(10),
LogMessage nvarchar(max),
MessageUid uniqueidentifier,
MessagePartUid uniqueidentifier,
MessagePartDataUid uniqueidentifier,
)
</text>
<ignoreFailures>false</ignoreFailures>
</install-command>
You can enable the installation log by overriding the default output stream in your InstallationContext
. For example, to have the installation log printed out to console, you can use:
LogManager.Configuration.Install(new InstallationContext(Console.Out));
You can also redirect the installation log to a string in memory:
var stringWriter = new StringWriter();
LogManager.Configuration.Install(new InstallationContext(stringWriter));
var installationLog = stringWriter.ToString(); // retrieve the installation output
Note that the log output can also be accessed via InstallationContext.LogOutput
:
// This is equivalent to the previous example
var installationContext = new InstallationContext(new StringWriter())
LogManager.Configuration.Install(installationContext);
var installationLog = installationContext.LogOutput.ToString();
Except for application critical exceptions (OutOfMemoryException. StackOverflowException and ThreadAbortException), LogManager.Configuration.Install()
will not rethrow any other exception, even when NLog is set up to throw exceptions.
- Troubleshooting Guide - See available NLog Targets and Layouts: https://nlog-project.org/config
- Getting started
- How to use structured logging
- Troubleshooting
- FAQ
- Articles about NLog
-
All targets, layouts and layout renderers
Popular: - Using NLog with NLog.config
- Using NLog with appsettings.json