Test
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using LoadTest.Common;
using log4net.Repository.Hierarchy;
using Logging.Services;
namespace LoadTest.Controllers
{
public class PerfmonLogTestController : Controller
{
AsyncLoggerService _service;
private object LogEnterpriseLibrary()
{
_service.LogError("test");
return null;
}
private object LogEnterpriseLibraryAsync()
{
_service.LogErrorAsync("test");
return null;
}
private object LogLog4Net()
{
log4net.LogManager.GetLogger("Name").Error("test");
return null;
}
private object LogLog4NetAsync()
{
LogHelper.Instance.Log("test");
return null;
}
public ActionResult Index()
{
_service = new AsyncLoggerService();
OptimizationHelper.CompareMethods(100, 5, 100, ControllerContext.HttpContext.Response.Output, "<br/>", LogEnterpriseLibrary, LogEnterpriseLibraryAsync, LogLog4Net, LogLog4NetAsync);
return new EmptyResult();
}
}
}
Result
Set0:
LogEnterpriseLibrary took: 1061308 ticks.
LogEnterpriseLibraryAsync took: 736623 ticks.
LogLog4Net took: 220974 ticks.
LogLog4NetAsync took: 2053 ticks.
Set1:
LogEnterpriseLibrary took: 620884 ticks.
LogEnterpriseLibraryAsync took: 752266 ticks.
LogLog4Net took: 58862 ticks.
LogLog4NetAsync took: 19 ticks.
Set2:
LogEnterpriseLibrary took: 659407 ticks.
LogEnterpriseLibraryAsync took: 710515 ticks.
LogLog4Net took: 41747 ticks.
LogLog4NetAsync took: 26 ticks.
Set3:
LogEnterpriseLibrary took: 812621 ticks.
LogEnterpriseLibraryAsync took: 632627 ticks.
LogLog4Net took: 41436 ticks.
LogLog4NetAsync took: 20 ticks.
Set4:
LogEnterpriseLibrary took: 649430 ticks.
LogEnterpriseLibraryAsync took: 660512 ticks.
LogLog4Net took: 60119 ticks.
LogLog4NetAsync took: 20 ticks.
Conclusion
log4net show higher performance than enterprise library.
How to use log4net
1. Get log4net library and add reference of it in your project.
2. Add line in your AssemblyInfo.cs file
[assembly: log4net.Config.XmlConfigurator(ConfigFile="Web.config", Watch=true)] //For log4net 1.2.10.0
3. Add following to web.config
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<log4net debug="true">
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="C:\\Log.txt" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="10MB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%-5p %d %5rms %-22.22c{1} %-18.18M - %m%n" />
</layout>
</appender>
<root>
<level value="DEBUG" />
<appender-ref ref="RollingLogFileAppender" />
</root>
</log4net>
4. Write code to log. Ex:
log4net.LogManager.GetLogger("Name").Error("test");
Hi, one question, this performance will be same for silverlight? because I'dont find log4Net to Silverlight, and can't add reference log4net in my silverlight project, silverlight don't have app.config or web.config file.
ReplyDelete