I'm totally new to Log4Net. I've been able to implement the below code that I found online. It's working fine in terms of capturing and saving data, i.e. date, the logged user, ip address,...
The only problem is that the Exception field is being blank. I want to capture the stack trace.
public void Application_Error(object sender, EventArgs e)
{
//Fires when an error occurs
var redirectUrl = "~/Error/GenericError.aspx";
var httpException = (HttpException)Server.GetLastError();
int httpCode = httpException.GetHttpCode();
log4net.GlobalContext.Properties("ipaddress")
= Request.ServerVariables("remote_addr");
log4net.GlobalContext.Properties("thread") = Request.Url.AbsoluteUri;
log4net.GlobalContext.Properties("userid") = User.Identity.Name;
var ctx = HttpContext.Current;
var _url = ctx.Request.Url.ToString();
log4net.GlobalContext.Properties("url") = _url;
log4net.GlobalContext.Properties("browser") = ctx.Request.Browser.Browser;
log4net.ILog log = log4net.LogManager.GetLogger(this.GetType());
log.Error(httpException.Message);
if ((httpCode == 404))
{
redirectUrl = "~/Error/FileNotFound.aspx";
}
else if ((httpCode == 403))
{
redirectUrl = "~/Error/UnauthorizedAccess.aspx";
}
else
{
var message = string.Empty;
if (httpException is HttpRequestValidationException)
{
message = "A potentially dangerous Request.Form value"
+ " was detected from the client.";
}
else
{
message = httpException.Message;
}
redirectUrl = "~/Error/GenericError.aspx?msg=" + message;
}
Server.ClearError();
Server.Transfer(redirectUrl);
}
This is the log4net portion of webconfig
<parameter>
<parameterName value="@exception" />
<dbType value="String" />
<size value="2000" />
<layout type="log4net.Layout.ExceptionLayout" />
</parameter>
Thanks for helping