简体   繁体   中英

Log4Net: logging all unhandled exceptions in C# class library project

I have a C# class library project and I need to capture all unhandled exceptions in my class even if I not use try catch blocks. Note: I have already installed log4net with log4net.config and it works fine logging manually in Debug,Info,Error.

Can you help me?

thanks

I had to do the same and this post was helpful. The only downside to this is that you may end up logging too many exceptions. Since you say you are writing a class library, depending on the application that will use your library, you may not want to catch all the exceptions thrown by the application. To solve this issue, I recommend doing something like:

AppDomain.CurrentDomain.FirstChanceException += (sender, e) => {
    if (e.Exception.TargetSite.DeclaringType.Assembly == Assembly.GetExecutingAssembly())
    {
        logger.ErrorFormat("Exception Thrown: {0}\n{1}", e.Exception.Message, e.Exception.StackTrace);
    }
};

This way, you will only catch exceptions from your code. Hope this helps !

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM