Jake Smith
Jake Smith

Reputation: 2813

How do I setup a FileAppender logger from log4net for a .NET Standard 2.0 class library?

How do I setup a File Appender logger from log4net for a class library using .NET Standard 2.0? I don't have an AssemblyInfo.cs. I think I have a fundamental misunderstanding that's leading to my confusion with this, so this may be a simple answer, but I just started a class library that I want to be accessible to as many projects as possible in our solutions and have internal logging from the start.

Upvotes: 2

Views: 1275

Answers (2)

Ali_Nass
Ali_Nass

Reputation: 928

Not sure if I understood what you need/want but this is an idea of how the config file should look like

   <log4net>
    <root>
      <level value="INFO" />
      <appender-ref ref="LogFileAppender" />
    </root>
    <appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender">
      <param name="File" value="test.txt" />
      <param name="AppendToFile" value="true" />
      <rollingStyle value="Size" />
      <maxSizeRollBackups value="10" />
      <maximumFileSize value="5MB" />
      <staticLogFileName value="true" />
      <layout type="log4net.Layout.PatternLayout">
        <param name="ConversionPattern" value="%date [%thread] %-5level %logger [%M %C] - %message%newline" />
      </layout>
  </log4net>

If i'm not mistaken, you don't need to add anything to your assemblyinfo (if you dont have one) but you need to add something like this on the startup

        log4net.Config.XmlConfigurator.Configure();

(in my case webapi so I add it to my startup.cs)

I hope this helps!

Upvotes: 0

Scott Chamberlain
Scott Chamberlain

Reputation: 127603

There is nothing special about the AssemblyInfo.cs file. The attribute to configure log4net can be put in any file in the assembly.

That being said I would recommend against using log4net directly and instead use a ILogger from the NuGet package Microsoft.Extensions.Logging.Abstractions. This allows the consumer of your library use any logging system they want instead of being tied to only using log4net.

Upvotes: 1

Related Questions