Select to view content in your preferred language

Example of Setting Up Logging (for Example NLog) with Add-In Project

478
2
Jump to solution
06-05-2024 10:23 PM
Gurunara
Frequent Contributor

Is there example of Setting Up Logging (for Example NLog) with Add-In Project?

Tags (1)
0 Kudos
1 Solution

Accepted Solutions
Gurunara
Frequent Contributor

Used log4net.

private static readonly log4net.ILog logger = log4net.LogManager.GetLogger(typeof(T));

log4net.Config.XmlConfigurator.Configure(new FileInfo("logger.config"));

logger.Debug("Test");


logger.config (eg): property "Copy Always" for this file has to be set, so it gets copied to bin build folder (wonder how this will get bundled with the esriAddinX file...)

<log4net>
<appender name="Console" type="log4net.Appender.ConsoleAppender">
<layout type="log4net.Layout.PatternLayout">
<!-- Pattern to output the caller's file name and line number -->
<conversionPattern value="%5level [%thread] (%file:%line) - %message%newline" />
</layout>
</appender>
 
<appender name="RollingFile" type="log4net.Appender.RollingFileAppender">
<!--<file type="log4net.Util.PatternString" value="c:\log\logname-%utcdate{yyyy-MM-dd-HH-mm-ss}.log" />-->
<file value="c:\log\EGIS.ALM.Pro.SourceNode.log" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maximumFileSize value="10000KB" />
<maxSizeRollBackups value="20" />
 
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%-4thread] %-5level %logger - %message%newline"/>
<!--<conversionPattern value="%date [%-4thread] %-5level %logger{1} - %message%newline"/>-->
</layout>
</appender>
 
<root>
<level value="DEBUG" />
<level value="WARN" />
<level value="INFO" />
<appender-ref ref="Console" />
<appender-ref ref="RollingFile" />
</root>
</log4net>
 
configured for each class library... however can put the static initializations into a class and call the class method.

View solution in original post

0 Kudos
2 Replies
Gurunara
Frequent Contributor

using nlog.config (at root level):

<?xml version="1.0" encoding="utf-8" ?>
<!-- XSD manual extracted from package NLog.Schema: https://www.nuget.org/packages/NLog.Schema-->
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xsi:schemaLocation="NLog NLog.xsd"
      autoReload="true"
      internalLogFile="c:\\logs\\console-example-internal.log"
      internalLogLevel="Info">
 
<!-- the targets to write to -->
<targets>
<!-- write logs to the files -->
<target xsi:type="File" name="all_logs_file" fileName="c:\\logs\\all.log"/>
<target xsi:type="File" name="important_logs_file" fileName="c:\\logs\\important.log"/>
<!-- write logs to the console-->
<target xsi:type="ColoredConsole" name="logconsole" />
</targets>
 
<!-- rules to map from logger name to target -->
<rules>
<logger name="*" minlevel="Trace" writeTo="logconsole" />
<logger name="*" minlevel="Debug" writeTo="all_logs_file" />
<logger name="*" minlevel="Warn" writeTo="important_logs_file" />
</rules>
</nlog>

and from constructor of viewmodel:

private static Logger logger = LogManager.GetCurrentClassLogger();

logger.Info("Some message");

but nothing gets output to the log directory c:\\logs\\
0 Kudos
Gurunara
Frequent Contributor

Used log4net.

private static readonly log4net.ILog logger = log4net.LogManager.GetLogger(typeof(T));

log4net.Config.XmlConfigurator.Configure(new FileInfo("logger.config"));

logger.Debug("Test");


logger.config (eg): property "Copy Always" for this file has to be set, so it gets copied to bin build folder (wonder how this will get bundled with the esriAddinX file...)

<log4net>
<appender name="Console" type="log4net.Appender.ConsoleAppender">
<layout type="log4net.Layout.PatternLayout">
<!-- Pattern to output the caller's file name and line number -->
<conversionPattern value="%5level [%thread] (%file:%line) - %message%newline" />
</layout>
</appender>
 
<appender name="RollingFile" type="log4net.Appender.RollingFileAppender">
<!--<file type="log4net.Util.PatternString" value="c:\log\logname-%utcdate{yyyy-MM-dd-HH-mm-ss}.log" />-->
<file value="c:\log\EGIS.ALM.Pro.SourceNode.log" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maximumFileSize value="10000KB" />
<maxSizeRollBackups value="20" />
 
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%-4thread] %-5level %logger - %message%newline"/>
<!--<conversionPattern value="%date [%-4thread] %-5level %logger{1} - %message%newline"/>-->
</layout>
</appender>
 
<root>
<level value="DEBUG" />
<level value="WARN" />
<level value="INFO" />
<appender-ref ref="Console" />
<appender-ref ref="RollingFile" />
</root>
</log4net>
 
configured for each class library... however can put the static initializations into a class and call the class method.
0 Kudos