I have C# (.NET 3.5) Winforms app, where I use NLog. I want to use RichTextBox target but getting error.
<targets>
<target name="file" xsi:type="File" fileName="${basedir}/AppLog.txt" />
<target xsi:type="RichTextBox"
name="rtbLog1"
layout="${message}"
height="800"
autoScroll="true"
maxLines="0"
showMinimized="false"
toolWindow="false"
controlName="rtb2"
formName="frmConverter"
width="800"
useDefaultRowColoringRules="false">
<word-coloring backgroundColor="Blue" fontColor="Red" ignoreCase="true"
regex="Error" style="Regular" text="Error"
wholeWords="true"/>
<!--repeated-->
<row-coloring backgroundColor="Blue" condition="Condition" fontColor="Red"
style="Regular"/>
<!--repeated-->
</target>
</targets>
<rules>
<logger name="*" minlevel="Debug" writeTo="file" />
<logger name="*" minlevel="Debug" writeTo="rtbLog1" />
</rules>
The error thrown is:
The type initializer for 'app.program' threw an exception.
Any help would be much appreciated!
Thanks.
Related
Using ASP.NET Core3.1 and NLog, this is a part of my NLog.config:
<nlog>
<targets>
<!-- write logs to file -->
<target name="allfile" xsi:type="File"
fileName="${aspnet-appbasepath}/AspNetCore_Nlog/nlog-all-${shortdate}.log"
layout="${longdate}|${event-properties:item=EventId_Id}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring} | url: ${aspnet-request-url} | controller: ${aspnet-mvc-controller} | action: ${aspnet-mvc-action}"
archiveAboveSize="1000000"
maxArchiveFiles="20" />
<target name="searchedFile" xsi:type="File"
fileName="${aspnet-appbasepath}/AspNetCore_Nlog/whatIsSearched-${shortdate}.txt"
layout="${longdate}|${event-properties:item=EventId_Id}|${uppercase:${level}}|${logger}|${message}"
archiveAboveSize="1000000"
maxArchiveFiles="20"/>
</targets>
<!-- rules to map from logger name to target -->
<rules>
<logger name="*" minlevel="Trace" writeTo="allfile" />
<logger name="whatIsSearched" minlevel="Trace" writeTo="searchedFile" />
</rules>
</nlog>
My problem is that, all the logs inside searchedFile, also exist in allfile.
I need to know if there is a way to exclude a special target from all logs/all file that logs every thing ?
I would move the logging-rule for whatIsSearched to the top, and add final="true":
<rules>
<logger name="whatIsSearched" minlevel="Trace" writeTo="searchedFile" final="true" />
<logger name="*" minlevel="Trace" writeTo="allfile" />
</rules>
See also: https://github.com/nlog/NLog/wiki/Configuration-file#rules
I have started using the Asp.Net Core 2.1 HttpClientFactory
services.AddHttpClient();
Now I get these huge logs from the HttpClient, every request logs Info and Trace level logs.
2018-11-08 14:34:59.6753|::1||INFO |8.11.7.0|12 |Dto|Log.RequestPipelineStart|Start processing HTTP request GET ...
I have been trying to throw them in a black hole:
<logger name="System.Net.Http.HttpClient*" minlevel ="Trace" writeTo="blackhole" final="true" />
But that does not help. Any suggestions?
Full Nlog:
<?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"
internalLogToConsoleError="true"
internalLogLevel="Warn"
internalLogFile="logs\internal-nlog.txt"
throwExceptions="true"
autoReload="true">
<!-- Load the ASP.NET Core plugin -->
<extensions>
<add assembly="NLog.Web.AspNetCore"/>
</extensions>
<!-- the targets to write to -->
<targets async="true">
<!-- write logs to file -->
<target xsi:type="File" name="allfile" fileName="logs\${machinename}-all.log"
layout="${longdate}|${aspnet-request-ip}|${aspnet-User-Identity}|${pad:padding=-5:${uppercase:${level}}}|${assembly-version}|${event-properties:item=EventId.Id}|${logger}|${aspnet-mvc-controller}|${aspnet-Request-Method}|${message}|${exception}"
maxArchiveFiles="7"
archiveFileName="logs\archive\${machinename}-all.{#}.zip"
archiveNumbering="Date"
archiveEvery="Day"
archiveDateFormat="yyyyMMdd"
enableArchiveFileCompression ="true"
/>
<!-- another file log, only own logs. Uses some ASP.NET core renderers -->
<target xsi:type="File" name="ownFile-trace" fileName="logs\${machinename}-trace.log"
layout="${longdate}|${aspnet-request-ip}|${aspnet-User-Identity}|${pad:padding=-5:${uppercase:${level}}}|${assembly-version}|${pad:padding=-3:${threadid}}|${aspnet-mvc-controller}|${callsite:className=True:includeNamespace=False:fileName=False:includeSourcePath=False:methodName=True:cleanNamesOfAnonymousDelegates=True:cleanNamesOfAsyncContinuations=True}|${message}|${exception}"
maxArchiveFiles="14"
archiveFileName="logs\archive\${machinename}-trace.{#}.zip"
archiveNumbering="Date"
archiveEvery="Saturday"
archiveDateFormat="yyyyMMdd"
enableArchiveFileCompression ="true"
/>
<!-- another file log, only own logs. Uses some ASP.NET core renderers -->
<target xsi:type="File" name="ownFile-error" fileName="logs\${machinename}-error.log"
layout="${longdate}|${aspnet-request-ip}||${aspnet-User-Identity}|${pad:padding=-5:${uppercase:${level}}}|${assembly-version}|${pad:padding=-3:${threadid}}|${aspnet-mvc-controller}|${callsite:className=True:includeNamespace=False:fileName=False:includeSourcePath=False:methodName=True:cleanNamesOfAnonymousDelegates=True:cleanNamesOfAsyncContinuations=True}|${message}|${exception:format=tostring}"
maxArchiveFiles="100"
archiveFileName="logs\archive\${machinename}-error.{#}.zip"
archiveNumbering="Date"
archiveEvery="Day"
archiveDateFormat="yyyyMMdd"
enableArchiveFileCompression ="true"
/>
<target xsi:type="File" name="ownFile-info" fileName="logs\${machinename}-info.log"
layout="${longdate}|${aspnet-request-ip}|${aspnet-User-Identity}|${pad:padding=-5:${uppercase:${level}}}|${assembly-version}|${aspnet-mvc-controller}|${callsite:className=True:includeNamespace=False:fileName=False:includeSourcePath=False:methodName=True:cleanNamesOfAnonymousDelegates=True:cleanNamesOfAsyncContinuations=True}|${message}|${exception}"
maxArchiveFiles="36"
archiveFileName="logs\archive\${machinename}-info.{#}.zip"
archiveNumbering="Date"
archiveEvery="Month"
archiveDateFormat="yyyyMMdd"
enableArchiveFileCompression ="true"
/>
<target xsi:type="ColoredConsole"
name="ColoredConsole"
layout="${aspnet-User-Identity}|${event-properties:item=EventId.Id}|${uppercase:${level}}|${logger}|${message} ${exception}"
header="${longdate}"
footer="${longdate}"
useDefaultRowHighlightingRules="True"
detectConsoleAvailable="True">
</target>
<target xsi:type="LimitingWrapper"
name="MailTarget"
messageLimit="1"
interval="00:15">
<target xsi:type="Mail"
layout="${longdate}|${aspnet-request-ip}||${aspnet-User-Identity}|${pad:padding=-5:${uppercase:${level}}}|${assembly-version}|${pad:padding=-3:${threadid}}|${aspnet-mvc-controller}|${callsite:className=True:includeNamespace=False:fileName=False:includeSourcePath=False:methodName=True}|${message}|${exception:format=tostring}"
subject ="Error in Testmij"
to="info#datec.nl"
from="testmijonline#testmij.nl"
body="${longdate}|${aspnet-request-ip}||${aspnet-User-Identity}|${pad:padding=-5:${uppercase:${level}}}|${assembly-version}|${pad:padding=-3:${threadid}}|${aspnet-mvc-controller}|${callsite:className=True:includeNamespace=False:fileName=False:includeSourcePath=False:methodName=True}|${message}|${exception:format=tostring}"
smtpUserName ="mail#smtp.datec.nl"
enableSsl ="false"
smtpPassword ="7dnV&q#D3s"
smtpAuthentication ="Basic"
smtpServer ="smtp.datec.nl "
smtpPort ="25"
/>
</target>
<!-- write to the void aka just remove -->
<target xsi:type="Null" name="blackhole" />
</targets>
<!-- rules to map from logger name to target -->
<rules>
<!--All logs, including from Microsoft-->
<logger name="*" minlevel="Trace" writeTo="allfile"/>
<!--Skip Microsoft logs and so log only own logs-->
<logger name="DTOWEB.Models.HttpClientExtensions" writeTo="blackhole" final="true" />
<logger name="Microsoft.*" minlevel="Trace" writeTo="blackhole" final="true" />
<logger name="System.Net.Http.HttpClient*" minlevel ="Trace" writeTo="blackhole" final="true" />
<logger name="*" minlevel="Error" writeTo="ownFile-error" />
<logger name="*" minlevel="Trace" writeTo="ownFile-trace" />
<logger name="*" minlevel="Info" writeTo="ownFile-info" />
<logger name="*" minlevel="Trace" writeTo="ColoredConsole" />
<logger name ="*" minlevel="Error" writeTo ="MailTarget"/>
</rules>
</nlog>
Answered, see comment of Rolf Kristensen
When you run a debug session in localhost, Visual Studio does not deploy all files to the bin directory. So the changed Nlog.config was not deployed.
I have the following nlog.config file for my project. When I debug locally, it works as expected, that Hangfire messages are being filtered to only show Warn and above. However on our staging server (IIS 8.5) nlog seems to ignore the rule and just logs everything (including Info) to elmah:
<?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">
<extensions>
<add assembly="NLog.Elmah"/>
</extensions>
<targets>
<target xsi:type="Elmah" name="elmahWithLogLevelAsType" layout="${message}" LogLevelAsType="true"/>
</targets>
<rules>
<logger name="Hangfire.*" minlevel="Warn" writeTo="elmahWithLogLevelAsType" final="true" />
<logger name="*" minlevel="Info" writeTo="elmahWithLogLevelAsType" />
</rules>
</nlog>
Even if I remove the Hangfire.* rule and change the catchall to minlevel="Warn" it still logs Info items.
Think you are running two different versions of NLog.
This will capture all log-events with warning (and above levels):
<logger name="Hangfire.*" minlevel="Warn" writeTo="elmahWithLogLevelAsType" final="true" />
This means all log-events with info or below will try the next rule:
<logger name="*" minlevel="Info" writeTo="elmahWithLogLevelAsType" />
Try this configuration instead:
<?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">
<extensions>
<add assembly="NLog.Elmah"/>
</extensions>
<targets>
<target xsi:type="Elmah" name="elmahWithLogLevelAsType" layout="${message}" LogLevelAsType="true"/>
</targets>
<rules>
<logger name="Hangfire.*" minlevel="Warn" writeTo="elmahWithLogLevelAsType" final="true" />
<logger name="Hangfire.*" maxLevel="Warn" final="true" /> <!-- BlackHole -->
<logger name="*" minlevel="Info" writeTo="elmahWithLogLevelAsType" />
</rules>
</nlog>
I'm using below configuration and expect my error logs go through email, but they don't
<targets>
<target name="traceLogger" xsi:type="File" fileName="C:\Temp\trace.log" layout="${date:format=G} ${level} ${message}"/>
<target name="warnLogger" xsi:type="File" fileName="C:\Temp\warning.log" layout="${date:format=G} ${message}"/>
<target name="infoLogger" xsi:type="File" fileName="C:\Temp\info.log" layout="${date:format=G} ${message}"/>
<target name="errorLogger" xsi:type="Mail"
subject="QC Result Processing Error"
to="name#domain.com"
bcc=""
cc=""
from="sender#domain.com"
smtpServer="smtpinfo"/>
</targets>
<rules>
<logger name="*" writeTo="traceLogger"></logger>
<logger name="*" levels="Warn" writeTo="warnLogger"></logger>
<logger name="*" levels="Info" writeTo="infoLogger"></logger>
<logger name="*" levels="Error" writeTo="errorLogger"></logger>
</rules>
I enabled nlog exception and found the problem. The issue was the blank bcc and cc. I removed them and the emailing worked fine.
I have three targets - Engine, Tasks and Error. Please, find belog their NLog configuration:
<targets>
<target name="EngineLog" xsi:type="File" fileName="C:\Log\EngineLog.txt" layout="${layout}"/>
<target name="ErrorLog" xsi:type="File" fileName="C:\Log\ErrorLog.txt" layout="${layout}"/>
<target name="TasksLog" xsi:type="File" fileName="C:\Log\TasksLog.txt" layout="${layout}"/>
<target name="ConsoleLog" xsi:type="ColoredConsole" layout="${consoleLayout}"/>
</targets>
<rules>
<logger name="*" minLevel="Error" writeTo="ErrorLog"/>
<logger name="N1.*" minLevel="Warn" writeTo="EngineLog" final="true"/>
<logger name="N2.*" minLevel="Info" writeTo="EngineLog" final="true" />
<logger name="N3.*" minLevel="Info" writeTo="EngineLog" final="true" />
<logger name="N4.*" minLevel="Info" writeTo="EngineLog" final="true" />
<logger name="N5.*" minLevel="Info" writeTo="EngineLog" final="true" />
<logger name="N6" minLevel="Info" writeTo="EngineLog" />
<logger name="*" minlevel="Info" writeTo="TasksLog" />
</rules>
Of course, the real namespace names are not N1...N6, what matters is that I have:
5 namespaces which are logged to the Engine log exclusively (N1-N5)
All the errors (both Engine and Tasks) are logged to the same Error log in addition to the respective dedicated target.
One namespace is logged both to Engine and Tasks (N6)
The rest is considered Tasks
Now I would like additionally to log everything going to either ErrorLog or TasksLog to the console.
My first try was to wrap the ErrorLog and TasksLog with a SplitGroup grouping each target with the console target, like this:
<target name="ErrorLog" xsi:type="SplitGroup">
<target xsi:type="File" fileName="C:\Log\ErrorLog.txt" layout="${layout}"/>
<target xsi:type="ColoredConsole" layout="${consoleLayout}"/>
</target>
<target name="TasksLog" xsi:type="SplitGroup">
<target xsi:type="File" fileName="C:\Log\TasksLog.txt" layout="${layout}"/>
<target xsi:type="ColoredConsole" layout="${consoleLayout}"/>
</target>
But this is a wrong approach, because every error gets logged twice on the console - first on behalf of the ErrorLog and then on behalf of the TasksLog.
How can I log errors to the console without duplications while allowing them to go to both the ErrorLog target and the TasksLog/EngineLog targets?
EDIT
The desired effect can be achieved if I could specify to log messages, but not if they are Error or Fatal (because these have already been logged).
OK, I think I have found the solution:
<target name="ErrorLog" xsi:type="SplitGroup">
<target xsi:type="File" fileName="C:\Log\ErrorLog.txt" layout="${layout}"/>
<target name="ConsoleLog" xsi:type="ColoredConsole" layout="${consoleLayout}"/>
</target>
And add the following rule at the end:
<logger name="*" levels="Warn, Info, Debug, Trace" writeTo="ConsoleLog" />