Skip to content

Instantly share code, notes, and snippets.

@wantedfast
Last active December 3, 2019 05:01
Show Gist options
  • Select an option

  • Save wantedfast/d0d360ef771bacf508f68fd9431efbe3 to your computer and use it in GitHub Desktop.

Select an option

Save wantedfast/d0d360ef771bacf508f68fd9431efbe3 to your computer and use it in GitHub Desktop.
Provide a workaround to logging file in .NET Core using Log4net.
## Install Log4net via Nuget
Open Nuget package manager then download Log4Net into your project.
## Create log4net.config file
Add a new Application configuration file in your .NET Core project, and named it as 'log4net.config', It will be used later.
Here is a template you can take a reference.
```
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<!-- This section contains the log4net configuration settings -->
<log4net>
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="<!--The Log File Path-->" />
<param name="AppendToFile" value="true" /><!--Logging file will overwrite previous log-->
<param name="MaxSizeRollBackups" value="100" /><!--Amount of Backup log files-->
<param name="MaxFileSize" value="1024" /><!--Log file size-->
<param name="StaticLogFileName" value="false" /><!--Static log file-->
<param name="DatePattern" value="yyyyMM/dd&quot;.log&quot;" />
<param name="RollingStyle" value="Date" /><!--Create file with date-->
<!--Error Log Layout-->
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%nLog Date:%d [%t] %nLog Level:%-5p %nDescrption:%c [%x] %n%m %n " />
</layout>
</appender>
<!-- Setup the root category, add the appenders and set the default level -->
<root>
<level value="ALL" />
<appender-ref ref="RollingLogFileAppender" />
</root>
</log4net>
</configuration>
```
## Code part
Add below code in a .cs file
// Add dependence into assembly. Imporatant !!
[assembly: log4net.Config.XmlConfigurator(Watch = true)]
namespace test.xx
{
public class LogHelper
{
private static ILoggerRepository repository;
private static bool repositoryCreated = false;
// The method will be called to logging file
public static void WriteLog(string msg)
{
// Create a repository then set this flag to true.
// This step is very important, otherwise an exception will throw when your just create a repository directly.
// The key reason is when this method be called again, the repository will be created again then an 'Repository be defined'
// exception throw.
if (!repositoryCreated)
{
repository = LogManager.CreateRepository("NETCoreRepository");
repositoryCreated = true;
}
else
{
repository = LogManager.GetRepository("NETCoreRepository");
}
// Read the log4net configuration
XmlConfigurator.Configure(repository, new FileInfo("log4net.config"));
ILog log = LogManager.GetLogger(repository.Name, "<DescrptionaYouWant>");
// You can define any log level you want. Like info, error, fatal and etc..
log.Error(msg);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment