How to enable log4net for class library?

I want to implement the logging function in the class library, which itself is mentioned in the web service. I tried adding app.config and did everything necessary, but it seems that when an exception is thrown, log4net just does nothing.

my app.config

<configSections> <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/> </configSections> <log4net> <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender"> <file value="D:\\mylogfile.txt" /> <appendToFile value="true" /> <rollingStyle value="Size" /> <maxSizeRollBackups value="5" /> <maximumFileSize value="10MB" /> <staticLogFileName value="true" /> <filter type="log4net.Filter.StringMatchFilter"> <stringToMatch value="test" /> </filter> <filter type="log4net.Filter.StringMatchFilter"> <stringToMatch value="error" /> </filter> <filter type="log4net.Filter.DenyAllFilter" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date [%thread] %level %logger - %message%newline%exception" /> </layout> </appender> <root> <level value="INFO"/> <appender-ref ref="RollingFileAppender"/> <appender-ref ref="ConsoleAppender" /> </root> </log4net> 

in AssemblyInfo.cs:

 [assembly: log4net.Config.XmlConfigurator(ConfigFile = "app.config")] 

in LogManager.cs:

 private static readonly ILog Log = log4net.LogManager.GetLogger (MethodBase.GetCurrentMethod().DeclaringType); public static void WriteLog(Exception ex) { Log.Error(ex); } 

Could you tell me what happened? How can I get log4net to work in my class library?

thanks

+11
c # log4net log4net-configuration
source share
3 answers

At run time, the configuration file is always used from the host application, unless explicitly specified. Here in case web.config is used, not app.cofig. Rather, mention a different custom configuration file name and make sure the file is copied to the web service virtual directory. Also, as they say, provide permission to the log file. Better save it in the app_data folder for the web service host.

+6
source share

You can use some kind of injection, or you build a simple one, for example:

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

And then you just enter something that matches this interface, like a wrapper for log4net or the like.

But, I think, the most correct thing is not to enter the class library. Why do I think so, because the library itself is not an application, your web service so your web service must decide what to do in the journal. If you want to log exceptions, just don't catch them in the class library and let your web service handle logging. It will also simplify the centralization of logging.

+3
source share

Please see my answer to the following question:

Use log4net in SDK

It has a log configuration procedure that builds the full path to the log4net configuration file. Since you are in a web service, I do not look where you expect "app.config".

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

+1
source share

All Articles