How to include log4net for a class library?

拟墨画扇 提交于 2019-11-29 09:31:36

At runtime, config file is always used from host application, unless declared explicitly. Here in case, web.config is being used not app.cofig. Rather mention some other custom config file name and ensure that file is copied in virtual directory of web service. Also as chibacity said, ensure permission on log file. Better keep it in app_data folder for web service host.

Tomas Jansson

You could use some kind of injection, either you build a simple one like:

public interface ILogger
{
    void LogInfo(string message);
       .
       .
       .
}

And then you just inject something that matches that interface, like a wrapper for log4net or such.

But, I think the most correct thing is to not log in the class library. Why I think so is because the library itself is not the application, your web service is so your web service should decide what to log. If you want to log exceptions just don't catch them in the class library and let your web service handle the logging. This will also make it easier to centralize the logging.

Tim Lloyd

Please see my answer to the following question:

Use log4net in SDK

It has a log configuration routine that will construct a complete path to a log4net config file. As you are in a webservice it my not be looking where you expect for "app.config".

Also make sure that you have permission to write to "D:\mylogfile.txt" from your webservice.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!