On 2011-10-25, Venkatasamy, Vanitha wrote:
> I am using log4net to log the errors in my application . Everything works
> fine for the initial setup and logging. Now I want to set the Category and
> Event in the Event log.
> I am using the below code to log the EventID ,but not sure about the
> category.
> log4net.ThreadContext.Properties["EventID"] = 5;
This should work using the Category property as you later show in your
own post. This is a new feature of 1.2.11, though, the category is
fixed in 1.2.10 and earlier.
> log4net.ThreadContext.Properties["Category"] = "500";
should work as well as
log4net.ThreadContext.Properties["Category"] = 500;
> Also how to differentiate the error levels [like i am getting the
> error message from Global.asax and logging the message to
> Eventlogs.But all the errors are going to log.error as below
Not sure I follow you here.
> if ((log.IsErrorEnabled) && ((checkException.GetHttpCode() == 500)
> || (checkException.GetHttpCode() == 404)))
> {
> if(checkException.GetHttpCode() == 500)
> log4net.ThreadContext.Properties["Category"] = "500";
> else
> log4net.ThreadContext.Properties["Category"] = "404";
> log.Error("Error" + sbErrorMessage.ToString() );
> }
> else if (log.IsInfoEnabled)
> {
> log.Warn("Information" + sbErrorMessage.ToString());
> }
> else if (log.IsWarnEnabled)
> {
> log.Info("Warning" + sbErrorMessage.ToString());
> }
You have likely swapped IsInfoEnabled and IsWarnEnabled (as you use Warn
if IsInfoEnabled and vice versa). Also note that IsWarnEnabled implies
IsInfoEnabled so you never hit that last branch.
> In addition ,I am not quite clear aout getting the Error,Warning and
> Information separately ..Like if i get an "InvalidOperationException"
> it's an warning but the httpcode is 500 and it gets logged as
> "Error". I tried to get sbErrorMessage.innerExeception but its null.
You mean you want to switch the level on the type of exception raised?
Where is this code, inside Global.asax Application_Error method? Take a
look at Server.GetLastError and maybe unwrap (i.e. look at the
InnerException) if it is a HttpUnhandledException.
Stefan