Now, I'll show you "How can you implement log4net at your Biztalk project?" and "How can you call your logger from your orchestration?"
Firstly, you have to got log4net.dll. You have to add this dll as a reference file to your helper class.
Secondly, create a class for logger instance. You can find my logger class codes below :
using log4net;
using System.IO;
public class Logger
{
public static Logger MailLog = new Logger(LogType.Mail);
public static Logger DefaultLog = new Logger(LogType.Default);
public static Logger EventLog = new Logger(LogType.EventLogger);
public static Logger FileAppender = new Logger(LogType.FileAppender);
public static void InitializeProjectConfigurationLog4Net()
{
FileInfo configFile;
configFile = new FileInfo("C:\\yourlogger.config");
if (configFile.Exists)
{
log4net.Config.XmlConfigurator.Configure(configFile);
}
else
{
log4net.Config.XmlConfigurator.Configure();
}
}
protected ILog MyLogger
{
get { return this._logger; }
}
private ILog _logger;
private Logger(string loggerName)
{
this._logger = LogManager.GetLogger(loggerName);
}
public void Debug(object message)
{
this.MyLogger.Debug(message);
}
public void Debug(object message, System.Exception t)
{
this.MyLogger.Debug(message, t);
}
public void Error(object message)
{
this.MyLogger.Error(message);
}
public void Error(object message, System.Exception t)
{
this.MyLogger.Error(message, t);
}
public void Fatal(object message)
{
this.MyLogger.Fatal(message);
}
public void Fatal(object message, System.Exception t)
{
this.MyLogger.Fatal(message, t);
}
public void Info(object message)
{
this.MyLogger.Info(message);
}
public void Info(object message, System.Exception t)
{
this.MyLogger.Info(message, t);
}
public void Warn(object message)
{
this.MyLogger.Warn(message);
}
public void Warn(object message, System.Exception t)
{
this.MyLogger.Warn(message, t);
}
}
public class LogType
{
public const string Mail = "SmtpLogger";
public const string Default = "DefaultLogger";
public const string EventLogger = "EventLogger";
public const string FileAppender = "FileAppender";
}
There is a static method in this class which is called InitializeProjectConfigurationLog4Net. This method makes your logger configuration. In this method you can see this line :
configFile = new FileInfo("C:\\yourlogger.config")
This is your logger configuration file. You have to put your log4net config information to this file. For example(yourlogger.config);
<?xml version="1.0"?><configuration><configSections><section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"/></configSections><appSettings><add key="log4net.Internal.Debug" value="true"/></appSettings><log4net><appender name="SmtpLogger" type="log4net.Appender.SmtpAppender"><to value="smbd@test.com"/><from value="from@test.com"/><subject value="test subject"/><smtpHost value="yoursmtpIP"/><bufferSize value="1024"/><lossy value="true"/><evaluator type="log4net.Core.LevelEvaluator"><threshold value="INFO"/></evaluator><layout type="log4net.Layout.PatternLayout"><conversionPattern value="%newline%date [%thread] %-5level %logger [%property{NDC}] - %message%newline%newline%XXX"/></layout></appender><appender name="FileAppender" type="log4net.Appender.FileAppender"><file value="c:\\your_log.txt"/><appendToFile value="true"/><layout type="log4net.Layout.PatternLayout">--><!--<conversionPattern value="%date - %-5level - %message%newline"/>--><!--<conversionPattern value="%-5level %date{yyyy-MM-dd HH:mm:ss} : %message%newline"/></layout></appender><appender name="EventLogger" type="log4net.Appender.EventLogAppender"><param name="LogName" value="yourLogName"/><param name="ApplicationName" value="yourApplicationName"/><layout type="log4net.Layout.PatternLayout"><param name="ConversionPattern" value="%d [%t] %-5p %c [%x] - %m %n"/></layout></appender><logger name="EventLogger"><appender-ref ref="EventLogger"/><level value="ALL"/></logger><logger name="FileAppender"><appender-ref ref="FileAppender"/><level value="ALL"/></logger><logger name="SmtpLogger"><appender-ref ref="SmtpLogger"/><level value="ALL"/></logger></log4net></configuration>
This file contains smpt logger, file logger and event viewer logger. After these definitions, you have to initalize your logger class . You can trigger this static initializer method from your orchestration.
logger = new HelperClass.Loggerlogger.InitializeProjectConfigurationLog4Net()
This expression starts your log4net logger. You can use your logger easily at your biztalk expressions.
logger.FileAppender.Info ("File not found");logger.MailLog.Info("Alert");
I have tried your code but the orchestration is throwing '.Logger' has no constructors defined, any ideas
ReplyDelete