Background
log4net is a framework for documenting or processing errors and messages in your application. Allowing you to send errors and alerts to files, email, udp/tcp broadcasts and more. More details can be found on log4net’s website. log4net was added to DotNetNuke framework in version 6.0 which included updates to the source for sending general alerts to such as when a file is requested as well as parlaying the Exceptions.LogException() errors from the event viewer to log4net.
Getting Started – Monitoring
By default log4net is not enabled and the configuration is completely up to you but there are a few basics that you should utilize. To get started follow the following steps
- Create a configuration file in your application root (same folder as your web.config) called DotNetNuke.log4net.config
- Save the following configuration:
<log4net>
<appender name="RollingFile" type="log4net.Appender.RollingFileAppender">
<file value="Portals/_default/Logs/" />
<datePattern value="yyyy.MM.dd'.log.resources'" />
<rollingStyle value="Date" />
<staticLogFileName value="false" />
<appendToFile value="true" />
<param name="Threshold" value="ERROR" />
<maximumFileSize value="10MB" />
<maxSizeRollBackups value="5" />
<lockingModel type="log4net.Appender.FileAppender+MinimalLock"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%property{log4net:HostName}][Thread:%thread][%level] %logger - %message%newline" />
<locationInfo value="true" />
</layout>
</appender>
<root>
<level value="ALL" />
<appender-ref ref="RollingFile" />
</root>
</log4net>
This configuration will give you daily date stamped log files in your /Portals/_default/Logs directory containing much of the same data available in your DNN Event Viewer.
A nice feature of the DotNetNuke.log4net.config implementation is the that you do not need to reset the application or cache for any changes to take effect. Nor does it automatically restart your application. You can enable and disable appenders on the fly.
Live monitoring of your application can be extremely valuable in development as well as in production to identify performance issues or attacks on your websites. With a few tweaks to the above configuration we can add another appender that will give us UDP broadcasts of errors.
<log4net>
<appender name="RollingFile" type="log4net.Appender.RollingFileAppender">
...
</appender>
<appender name="UDPBroadcast" type="log4net.Appender.UdpAppender">
<param name="RemoteAddress" value="localhost" />
<param name="RemotePort" value="877" />
<param name="Threshold" value="ALL" />
<layout type="log4net.Layout.XmlLayout">
<locationInfo value="true" />
</layout>
</appender>
<root>
<level value="ALL" />
<appender-ref ref="RollingFile" />
<appender-ref ref="UDPBroadcast" />
</root>
</log4net>
Now with a program like log4view we can consume these broadcasts and watch for any errors or alerts to see where in a stack our application may be and if any errors have arisen without reopening files or refreshing the DNN event viewer.
If you host multiple websites and you want to monitors errors for all of them in a central place then UDP broadcasting is a great way to aggregate them all.
Adding log4net to your modules
The core DNN framework is utilizing log4net but your custom built applications will get the benefits from Exception.LogException() function posting to log4net automatically but you can implement more granular alerts by referencing log4net in your application.
- Add a Reference to DotNetNuke.Log4Net.dll located in the bin directory of DotNetNuke
- Now simply send the type of message alert you would like
- DnnLog.Info(“Starting a really complex process! Wish me luck!”)
- DnnLog.MethodEntry() or DnnLog.MethodExit() to send details about the function you’ve just entered or exited.
- See the full list of public functions on right.