meisenstein | 11 Jul 2011 15:42

Log4Net Error on DB Connect AdoNetAppender


I have my AdoNetAppender as code instead of as a XML config file, 
 log4net.Appender.AdoNetAppender adoAppender = new
log4net.Appender.AdoNetAppender();

I would like to catch an error if the database it's trying to connect to is
down, so that I can switch over to the file appender. This is where my
problem arises, Log4Net seems to consume the errors and echo the error to
the console instead of throwing it back again. I've wrapped my
AdoNetAppender in a try catch:

 
catch (Exception ex)
      {
        LoadFileAppender(level);
        Log log = new Log("AdoAdaptorFail");
        log.Error("AdoAdaptor Failed", "System", ex);
      }

to load the file appender and log the error, however ..it never comes here
because Log4Net consumes the error and writes it to the console...
Is there ANY WAY i can catch this configuration error to load the file
appender? 
Thanks

--

-- 
View this message in context: http://old.nabble.com/Log4Net-Error-on-DB-Connect-AdoNetAppender-tp32037558p32037558.html
Sent from the Log4net - Users mailing list archive at Nabble.com.

(Continue reading)

Ron Grabowski | 11 Jul 2011 18:39
Picon
Favicon

Re: Log4Net Error on DB Connect AdoNetAppender

Try adding an ErrorHandler on the appender:

http://logging.apache.org/log4net/release/sdk/log4net.Core.IErrorHandler.html

AdoNetAppender adoAppender = new AdoNetAppender();
adoAppender.ErrorHandler = new FileAppenderErrorHandler();

From: meisenstein <meisenstein <at> connectivityreport.com>
To: log4net-user <at> logging.apache.org
Sent: Monday, July 11, 2011 9:42 AM
Subject: Log4Net Error on DB Connect AdoNetAppender


I have my AdoNetAppender as code instead of as a XML config file,
log4net.Appender.AdoNetAppender adoAppender = new
log4net.Appender.AdoNetAppender();


I would like to catch an error if the database it's trying to connect to is
down, so that I can switch over to the file appender. This is where my
problem arises, Log4Net seems to consume the errors and echo the error to
the console instead of throwing it back again. I've wrapped my
AdoNetAppender in a try catch:


catch (Exception ex)
      {
        LoadFileAppender(level);
        Log log = new Log("AdoAdaptorFail");
        log.Error("AdoAdaptor Failed", "System", ex);
      }


to load the file appender and log the error, however ..it never comes here
because Log4Net consumes the error and writes it to the console...
Is there ANY WAY i can catch this configuration error to load the file
appender?
Thanks

--
View this message in context: http://old.nabble.com/Log4Net-Error-on-DB-Connect-AdoNetAppender-tp32037558p32037558.html
Sent from the Log4net - Users mailing list archive at Nabble.com.



meisenstein | 12 Jul 2011 21:11

Log4Net Inserting Wrong Data


I have a wrapper I made for log4net, it has Debug, Info, Warn, Error, and
Fatal methods.
In my log.info it does not even take in an exception as it shouldn't being
an information.
The first DB insert works perfectly:
Id	Date	   Thread	Level	AppId	Logger	Class	Method	Message	Type	Exception
User
44	2011-07-12 14:54:53.470	10	INFO	TestApplication	NULL	(null)	(null)	Info
msg  (null)		meisenstein

However after an insert with a type and class and method (any Warn, Error,
Fatal) The next time log.Info("info msg") is called, it keeps the previous
information:

Id	Date	Thread	Level	AppId	Logger	Class	Method	Message	Type	Exception	User
48	2011-07-12 14:56:04.387	10	INFO	TestApplication	NULL	Program	Main	Info
msg	IndexOutOfRangeException		meisenstein

47	2011-07-12 14:55:24.977	10	FATAL	TestApplication	NULL	Program	Main	Fatal
msg	IndexOutOfRangeException	System.IndexOutOfRangeException: Index was
outside the bounds of the array.     at
CDS.Core.Logging.Program.Main(String[] args) in
C:\SourceControl\CDS.Core.Logging\TestProgram\Program.cs:line 21	meisenstein

Notice how type was null before, but now it's IndexOutOfRangeException.

I have this method which sets these custom fields:
  private void SetExceptionDetail(string userId, Exception ex)
        {
            log4net.GlobalContext.Properties["appId"] = ApplicationName;

            if (userId != null)
                log4net.GlobalContext.Properties["user"] = userId;

            if (ex != null)
            {
                log4net.GlobalContext.Properties["type"] =
ex.GetType().Name;

                log4net.GlobalContext.Properties["method"] =
GetCallingMethodName();

                if (ex.TargetSite != null)
                {
                    if (ex.TargetSite.DeclaringType != null)
                        log4net.GlobalContext.Properties["class"] =
ex.TargetSite.DeclaringType.Name;
                }
            }
        }

But in info, the last 3 are skipped because ex is null

 public void Info(string message, string userName, params MailAddress[]
additionalRecipients)
        {
            if (LogLevel >= Level.Info)
            {
                try
                {
                    SetExceptionDetail(userName, null);

                    log4NetLogger.Info(message);
                }
                catch (Exception e)
                {
                    SendEmail(Level.Fatal, "Failed to log info event",
userName, e);
                }
            }
        }

any ideas on how to clear it to make sure that the previous stuff does not
get inserted when I call the info method?

--

-- 
View this message in context: http://old.nabble.com/Log4Net-Inserting-Wrong-Data-tp32048451p32048451.html
Sent from the Log4net - Users mailing list archive at Nabble.com.

rebelrajesh | 14 Jul 2011 13:19
Picon

Log4net Adonet Appender: getting data jumbled when multiple instances of an application are runned


Scenario: A asp.net web application was deployed on remote IIS and is
configured to log into oracle database.Log table has these
columns:Login_ID,Log_message,Querystring,ipaddress. So the deployed url was
browsed from different systems using different login_ids
Problem: When the application is runned on different systems with different
login Ids and different flows, login id  of one flow/system is getting
inserted for another flow/system. But when a single instance of the
application is runned, walues are properly inserted.
Example: With login id X, the application is browsed through a flow ‘A’ and
another user with loginid Y  is browsing the application through  flow ‘B’ .
Loginid ‘ X’ is getting inserted into records with flow ‘B’ and flow ‘A’ 
Code: 
user = (User)Session["User"];
log4net.GlobalContext.Properties["LOGIN_ID"] = user.loginid;
Research: After going through few sites, I learnt that log4net contexts
doesn’t function properly with asp.net. 
http://piers7.blogspot.com/2005/12/log4net-context-problems-with-aspnet.html
http://piers7.blogspot.com/2007/07/log4net-in-aspnet-redux-implement.html
http://piers7.blogspot.com/2005/11/threadstatic-callcontext-and_02.html

These links suggest me to use httpcontext, which i tried but unable to get
the result. So can anyone help me to solwe this problem.

Modified Code:
public class loginIdProvider
    {
        public override string ToString()
        {                       
            string temp = (string)(HttpContext.Current.Items["loginID"]);
            return temp;         
        }
    }
loginIdProvider obj = new loginIdProvider();
                        log4net.GlobalContext.Properties["LOGIN_ID"] =
obj.ToString();

--

-- 
View this message in context: http://old.nabble.com/Log4net-Adonet-Appender%3A-getting-data-jumbled-when-multiple-instances-of-an-application-are-runned-tp32060419p32060419.html
Sent from the Log4net - Users mailing list archive at Nabble.com.

SoxFan44 | 14 Jul 2011 22:16
Picon
Favicon

Pass in object to custom appender programmatically?


Hi,
I wrote a custom appender that needs a custom object to be passed in to it. 
I see how I can use the config file to pass in string values, but is there a
way to programmatically pass in objects?  Ex:

public class MyAppender : AppenderSkeleton
{
  public MyCustomObject obj { get; set; }

  public override Append(...)
  {
    ...
  }
}

Thanks!
--

-- 
View this message in context: http://old.nabble.com/Pass-in-object-to-custom-appender-programmatically--tp32064132p32064132.html
Sent from the Log4net - Users mailing list archive at Nabble.com.


Gmane