Skip to content

Instantly share code, notes, and snippets.

@itorian
Last active March 9, 2016 02:39
Show Gist options
  • Save itorian/5da2c5b32b16aec59871 to your computer and use it in GitHub Desktop.
Save itorian/5da2c5b32b16aec59871 to your computer and use it in GitHub Desktop.
## 1. Install log4net using NuGet
## 2. Add assembly in AssemblyInfo.cs file
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "Web.config", Watch = true)]
## 3. Add settings in web.config
<configuration>
<configSections>
:::
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
</configSections>
:::
<log4net>
<appender name="RollingFile" type="log4net.Appender.RollingFileAppender">
<file value="Logs/LogFile.txt" />
<appendToFile value="true" />
<maximumFileSize value="500KB" />
<maxSizeRollBackups value="2" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %level %logger - %message%newline" />
</layout>
</appender>
<root>
<level value="All" />
<appender-ref ref="RollingFile" />
</root>
</log4net>
</configuration>
## 4. Create CustomExceptionFilter.cs inside Filters folder on root
public class CustomExceptionFilter : HandleErrorAttribute
{
public override void OnException(ExceptionContext filterContext)
{
//if (filterContext.ExceptionHandled || !filterContext.HttpContext.IsCustomErrorEnabled)
//{
// return;
//}
//if (new HttpException(null, filterContext.Exception).GetHttpCode() != 500)
//{
// return;
//}
//if (!ExceptionType.IsInstanceOfType(filterContext.Exception))
//{
// return;
//}
// if the request is AJAX return JSON else view.
if (filterContext.HttpContext.Request.Headers["X-Requested-With"] == "XMLHttpRequest")
{
filterContext.Result = new JsonResult
{
JsonRequestBehavior = JsonRequestBehavior.AllowGet,
Data = new
{
error = true,
message = filterContext.Exception.Message
}
};
}
else
{
var controllerName = (string)filterContext.RouteData.Values["controller"];
var actionName = (string)filterContext.RouteData.Values["action"];
var model = new HandleErrorInfo(filterContext.Exception, controllerName, actionName);
filterContext.Result = new ViewResult
{
ViewName = View,
MasterName = Master,
ViewData = new ViewDataDictionary(model),
TempData = filterContext.Controller.TempData
};
}
log4net.ILog log = log4net.LogManager.GetLogger(filterContext.RouteData.Values["controller"].ToString());
var msg = filterContext.Exception.Message;
var ex = filterContext.Exception;
log.Error(ex.ToString() + " | " + msg);
filterContext.ExceptionHandled = true;
filterContext.HttpContext.Response.Clear();
filterContext.HttpContext.Response.StatusCode = 500;
filterContext.HttpContext.Response.TrySkipIisCustomErrors = true;
}
}
## 5. Register filter globally
public class FilterConfig
{
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
::::::::::::::::
filters.Add(new CustomExceptionFilter());
}
}
## 6. Create Logs folder on root of the project
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment