hgs yĆ¼kleme
My Cart (0)  |  My Orders  |  My Downloads  |  My Auction  |  My Account  |  Help


Login |Register        Search

How to use log4net with dotnetnuke

                Print      Add To Favorite     Add To Watch List     Contact Author

Creator: host   9/10/2012 10:07:32 PM    Author: Jonathan Sheely   Source: http://www.dotnetnuke.com/Resources/Blogs/EntryId/3474/Using-log4net-with-DotNetNuke.aspx   Views: 3712    0    0  
Tags:
Log4net Module develop Dnn development

Background

log4net_dnnlog4net 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.

image

 

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.image

  • 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.

Rating People: 11   Average Rating:     

     DnnModule.com is built to provide DNN quality modules and DNN skins, some of them are free, some not. We wish these stuffs (free or not ) can be useful to you.

     Besides that, we also provide a full range of professional services, ranging from web site build, seo, system management, administration, support, senior consultancy and security services. We act as if your development project or network was ours, with care and respect. We are not satisfied until it works the way you want it to, and we don't silently ignore found issues as somebody else's problem.