NLog allows you to log in for several purposes through a configuration file. Remember to set ignoreFailures to true to ensure that any install / uninstall errors are ignored:
<?xml version="1.0" encoding="utf-8" ?> <nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <targets> <target name="logfile" xsi:type="File" /> <target name="db" xsi:type="Database" /> </targets> <rules> <logger name="*" levels="Info" writeTo="logfile,db" /> </rules> </nlog>
See the database target in the NLog documentation for more information.
Edit: You can also create a custom goal to achieve this in code:
using NLog; using NLog.Targets; namespace MyNamespace { [Target("MyFirst")] public sealed class MyFirstTarget: TargetWithLayout { public MyFirstTarget() { this.Host = "localhost"; } [Required] public string Host { get; set; } protected override void Write(LogEventInfo logEvent) { string logMessage = this.Layout.Render(logEvent); SendTheMessageToRemoteHost(this.Host, logMessage); } private void SendTheMessageToRemoteHost(string host, string message) {
and use it with:
<nlog> <extensions> <add assembly="MyAssembly"/> </extensions> <targets> <target name="a1" type="MyFirst" host="localhost"/> </targets> <rules> <logger name="*" minLevel="Info" appendTo="a1"/> </rules> </nlog>
See this page for more details.
source share