Feedjit

Articles for you

Sunday, June 15, 2014

Log4net Implementation in CRM 2011, log4net in CRM Plugins, WebServices and Custom aspx Pages.

This post is about log4net logger implementation in CRM 2011. I will provide details about log4net in CRM Plugins. It is as the same with Web-services and Custom aspx Pages.


1. First we will have a Config file for log4net so that, its configuration/settings can be read and applied to logger.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
  </configSections>
  <log4net>
    <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
      <file value="D:\Logger\Log4net\logs\log.txt" />
      <appendToFile value="false" />
      <rollingStyle value="Size" />
      <maxSizeRollBackups value="5" />
      <maximumFileSize value="1GB" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
      </layout>
    </appender>
    <appender name="MemoryAppender" type="log4net.Appender.MemoryAppender">
    </appender>
    <root>
      <level value="Info" />
      <appender-ref ref="RollingLogFileAppender" />
      <appender-ref ref="MemoryAppender" />
    </root>
  </log4net>
</configuration
Copy all this into a file named "log4net.config" and place it in D:\Logger\Log4net\Config\

2. Add log4net.dll to your Plugin.

3.
using log4net;
using log4net.Config;
public class MyPlugin: IPlugin
    {
    protected static readonly ILog Logger = LogManager.GetLogger(typeof(PublishToGRID));
    static MyPlugin()
    {
      XmlConfigurator.Configure(new System.IO.FileInfo(@"D:\Logger\Log4net\Config\log4net.config"));
    }
try
{
if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
{
  Logger.Info("Plugin Executed");
}
}
catch (Exception ex)
{
  string ErrorMessage = "Exception Occured in CRM";
  Logger.Error(ErrorMessage,ex);              
}


After your plugin executed: you will se the log file generated in D drive.
containing the following text:

Logger.info: 6/15/2014  7:11 p.m
Plugin Excuted







Read More

Articles for you