Add log4net dll reference to your MVC project
------------------------------------------------------------------------------
Create following model inside your models folder
-------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
namespace MVCWebApp.Models
{
public class LoggingFilterAttribute : ActionFilterAttribute
{
#region Logging
/// <summary>
/// Access to the log4Net logging object
/// </summary>
protected static readonly log4net.ILog log =
log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
private const string StopwatchKey = "DebugLoggingStopWatch";
#endregion
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
if (log.IsDebugEnabled)
{
var loggingWatch = Stopwatch.StartNew();
filterContext.HttpContext.Items.Add(StopwatchKey, loggingWatch);
var message = new StringBuilder();
message.Append(string.Format("Executing controller {0}, action {1}",
filterContext.ActionDescriptor.ControllerDescriptor.ControllerName,
filterContext.ActionDescriptor.ActionName));
log.Debug(message);
}
}
public override void OnActionExecuted(ActionExecutedContext filterContext)
{
if (log.IsDebugEnabled)
{
if (filterContext.HttpContext.Items[StopwatchKey] != null)
{
var loggingWatch = (Stopwatch)filterContext.HttpContext.Items[StopwatchKey];
loggingWatch.Stop();
long timeSpent = loggingWatch.ElapsedMilliseconds;
var message = new StringBuilder();
message.Append(string.Format("Finished executing controller {0}, action {1}-time spent {2}",
filterContext.ActionDescriptor.ControllerDescriptor.ControllerName,
filterContext.ActionDescriptor.ActionName,
timeSpent));
log.Debug(message);
filterContext.HttpContext.Items.Remove(StopwatchKey);
}
}
}
}
}
------------------------------------------------------------------------------
Add following content to main Web.Config
-------------------------------------------------------------------------------
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<log4net debug="false">
<appender name="LogFileAppender" type="log4net.Appender.FileAppender">
<param name="File" value="C:\logs.txt" />
<param name="AppendToFile" value="true"/>
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %c %m%n" />
</layout>
</appender>
<root>
<level value="All" />
<appender-ref ref="LogFileAppender" />
</root>
</log4net>
------------------------------------------------------------------------------
Add LoggingFilter attribute to your respective controllers
-------------------------------------------------------------------------------
[LoggingFilter]
public class HomeController : Controller
{
}
------------------------------------------------------------------------------
Create following model inside your models folder
-------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
namespace MVCWebApp.Models
{
public class LoggingFilterAttribute : ActionFilterAttribute
{
#region Logging
/// <summary>
/// Access to the log4Net logging object
/// </summary>
protected static readonly log4net.ILog log =
log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
private const string StopwatchKey = "DebugLoggingStopWatch";
#endregion
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
if (log.IsDebugEnabled)
{
var loggingWatch = Stopwatch.StartNew();
filterContext.HttpContext.Items.Add(StopwatchKey, loggingWatch);
var message = new StringBuilder();
message.Append(string.Format("Executing controller {0}, action {1}",
filterContext.ActionDescriptor.ControllerDescriptor.ControllerName,
filterContext.ActionDescriptor.ActionName));
log.Debug(message);
}
}
public override void OnActionExecuted(ActionExecutedContext filterContext)
{
if (log.IsDebugEnabled)
{
if (filterContext.HttpContext.Items[StopwatchKey] != null)
{
var loggingWatch = (Stopwatch)filterContext.HttpContext.Items[StopwatchKey];
loggingWatch.Stop();
long timeSpent = loggingWatch.ElapsedMilliseconds;
var message = new StringBuilder();
message.Append(string.Format("Finished executing controller {0}, action {1}-time spent {2}",
filterContext.ActionDescriptor.ControllerDescriptor.ControllerName,
filterContext.ActionDescriptor.ActionName,
timeSpent));
log.Debug(message);
filterContext.HttpContext.Items.Remove(StopwatchKey);
}
}
}
}
}
------------------------------------------------------------------------------
Add following content to main Web.Config
-------------------------------------------------------------------------------
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<log4net debug="false">
<appender name="LogFileAppender" type="log4net.Appender.FileAppender">
<param name="File" value="C:\logs.txt" />
<param name="AppendToFile" value="true"/>
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %c %m%n" />
</layout>
</appender>
<root>
<level value="All" />
<appender-ref ref="LogFileAppender" />
</root>
</log4net>
------------------------------------------------------------------------------
Add LoggingFilter attribute to your respective controllers
-------------------------------------------------------------------------------
[LoggingFilter]
public class HomeController : Controller
{
}
kayseriescortu.com - alacam.org - xescortun.com
ReplyDeleteperde modelleri
ReplyDeletemobil onay
vodafone mobil ödeme bozdurma
nft nasıl alınır
ankara evden eve nakliyat
trafik sigortası
DEDEKTOR
site kurmak
ask kitaplari
SMM PANEL
ReplyDeleteSmm panel
iş ilanları
İnstagram takipçi satın al
Https://www.hirdavatciburada.com
beyazesyateknikservisi.com.tr
SERVİS
tiktok jeton hilesi indir
Sridevi Satta Penal Chart
ReplyDelete