i'm using log4net to create log files dynamicaly , i tried the following steps here http://geekswithblogs.net/rgupta/archive/2009/03/03/dynamic-log-filenames-with-log4net.aspx but i'm getting (null).txt file , Here is my log4net.config file
`<log4net>
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file type="log4net.Util.PatternString" value="app_data/log/%date{yyyy-MM-dd}/%property{LogName}.txt" />
<rollingStyle value="Date" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<datePattern value="yyyyMMdd" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="10000KB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value=" %-5level %date{dd-mm-yyyy HH:mm:ss} %logger [%thread] - %message %exception%newline%property{LogName}" />
</layout>
</appender>
<root>
<appender-ref ref="RollingFileAppender" />
<level value="All" />
</root>
</log4net>`
and this is my test code
log4net.GlobalContext.Properties["LogName"] = "xx#xx.xx";
As i said i'm getting (null).txt FileName instead of xx#xx.xx.txt , thanks for helping
I just tested your code and its working just fine, so I can only assume, that you are assigning your GlobalContext property to late. Remember, to set it before you call XmlConfigurator.Configure method. Set it up in your Program.cs file like this:
public static class Program
{
public static void Main(string[] args)
{
// ...
GlobalContext.Properties["LogName"] = "xx#xx.xx";
XmlConfigurator.Configure(new FileInfo("log4net.config"));
// ...
}
}
Related
I use Castle.Windsor and Log4Net for initialize my Logger.
My log4net.config:
<?xml version="1.0"?>
<configuration>
<log4net>
<appender name="AllMessagesFile" type="log4net.Appender.RollingFileAppender,log4net">
<file value="Logs/Log.log" />
<level value="ALL" />
<encoding value="utf-8" />
<appendToFile value="true" />
<rollingStyle value="Composite" />
<maximumFileSize value="100000KB" />
<maxSizeRollBackups value="-1" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%d %identity [%t] %-5p %c{1}.%M %m%n" />
<IgnoresException value="False" />
</layout>
</appender>
<appender name="ErrorsOnlyFile" type="log4net.Appender.RollingFileAppender,log4net">
<file value="Logs/Error.log" />
<level value="ERROR" />
<encoding value="utf-8" />
<appendToFile value="true" />
<rollingStyle value="Composite" />
<maximumFileSize value="100000KB" />
<maxSizeRollBackups value="-1" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%d %identity [%t] %-5p %c{1}.%M %m%n" />
</layout>
</appender>
<root>
<priority value="ALL" />
<appender-ref ref="AllMessagesFile" />
<appender-ref ref="ErrorsOnlyFile" />
</root>
</log4net>
</configuration>
My AssamblyInfo.cs have foolow row:
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", Watch = true)]
In constructor of class I got object via Winsdor:
public class ACIStart
{
private ILogger Logger { get; set; }
public ACIStart(ILogger logger)
{
Logger = logger;
}
I don't have any exceptions, I just got a logger with all properties (IsDebbugEnabled, IsErrorEnabled ... ) as false.
My log no write anything to log file.
What could be the problem?
It could be that you need to add the loglevel in you root node:
<root>
<level value="DEBUG" />
...
I want my logfile to end up like this :
2015-04-23.log (YYMMDD.log)
<root>
<level value="DEBUG" />
<appender-ref ref="RollingFileAppender" />
</root>
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file type="log4net.Util.PatternString" value="logs\CTI\ServiceName" />
<appendToFile value="true" />
<rollingStyle value="Date" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="20MB" />
<staticLogFileName value="false" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %level %logger - %message%newline%exception" />
</layout>
</appender>
After running the code, the log file comes up as ServiceName_2015-04-23.log.2015-04-23.
What is the issue?
The appender should look like this:
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="logs\CTI\ServiceName\" />
<datePattern value="yyyy-MM-dd'.log'" />
<appendToFile value="true" />
<rollingStyle value="Composite" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="20MB" />
<staticLogFileName value="false" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %level %logger - %message%newline%exception" />
</layout>
</appender>
The key is the datePattern where we add '.log' to the pattern.
Two years later... Better late than never.
You use rollingStyle=date, so log4net adds date in the end of file name.
You can find useful config examples for RollingFileAppender class here.
I used this config but a date is always added to the current file ('log.20130805.0.log').
<appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="Logs/logFile.log" />
<appendToFile value="true" />
<preserveLogFileNameExtension value="true" />
<rollingStyle value="Composite" />
<datePattern value=".yyyyMMdd" />
<maximumFileSize value="10MB" />
<countDirection value="1"/>
<maxSizeRollBackups value="-1" />
<staticLogFileName value="false" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%date [%thread] %-5level %logger - %message%newline" />
</layout>
</appender>
The result of that config is :
log.20130805.0.log
log.20130805.1.log
log.20130805.2.log
log.20130805.3.log
What I get with staticLogFileName = true is :
log.log
log.1.log
log.2.log
log.3.log
What I want is :
log.log
log.20130805.1.log
log.20130805.2.log
log.20130805.3.log
I know I'm late to the party, but I believe I have figured out how to use a composite RollingFileAppender properly. I'm posting it here for posterity, in case anyone stumbles upon this in a search:
<appender name="rollingfileappender" type="log4net.Appender.RollingFileAppender">
<file value="C:\Logs\logfile.log" />
<datePattern value=".yyyyMMdd" />
<preserveLogFileNameExtension value="true" />
<appendToFile value="true" />
<rollingStyle value="Composite" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="100Kb" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.DynamicPatternLayout">
<conversionPattern value="%[level] - %date - %message" />
</layout>
</appender>
The key here is:
<preserveLogFileNameExtension value="true" />
and
<staticLogFileName value="true" />
When your current log files are being appended, you'll see:
logfile.log
logfile.1.log
logfile.2.log
etc...
Once the new day hits, those should be renamed to:
logfile.20200623.log
logfile.20200623.1.log
logfile.20200623.2.log
etc...
and you'll have a new logfile.log, still catching your current logs.
You can use the function below.
This function first gets the file location that you set in web.config and after that you can add any path that you want! (like Date or Customer or...)
WebConfig:
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="C:\\t4\\"/>
<appendToFile value="true"/>
<rollingStyle value="Composite"/>
<datePattern value="_yyyy-MM-dd.lo'g'"/>
<maxSizeRollBackups value="10"/>
<maximumFileSize value="1MB"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date User:%identity IP:%X{addr} Browser: %X{browser} Url: %X{url} [%thread] %-5level %c:%m%n"/>
</layout>
</appender>
Function:
public static void ChangeFileLocation(string _CustomerName,string _Project)
{
XmlConfigurator.Configure();
log4net.Repository.Hierarchy.Hierarchy h =(log4net.Repository.Hierarchy.Hierarchy)LogManager.GetRepository();
foreach (IAppender a in h.Root.Appenders)
{
if (a is FileAppender)
{
FileAppender fa = (FileAppender)a;
string sNowDate= DateTime.Now.ToLongDateString();
// Programmatically set this to the desired location here
string FileLocationinWebConfig = fa.File;
string logFileLocation = FileLocationinWebConfig + _Project + "\\" + CustomerName + "\\" + sNowDate + ".log";
fa.File = logFileLocation;
fa.ActivateOptions();
break;
}
}
}
and result is like this: C:\t4\TestProject\Customer1\Saturday, August 31, 2013.log
Based on these tips I guess it isn't possible, unfortunately (with the current implementation of RollingFileAppender):
http://geekswithblogs.net/rgupta/archive/2009/03/03/tips-on-using-log4net-rollingfileappender.aspx
Here is the appender I used in app.config, it will generate files like 20120501.log.1, while what I expected is 20120501.1.log.
<appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender">
<lockingModel type="log4net.Appender.FileAppender+MinimalLock"/>
<file value="logs\" />
<appendToFile value="true" />
<rollingStyle value="Composite" />
<datePattern value="yyyyMMddt'.log.'" />
<countDirection value="1" />
<maxSizeRollBackups value="100" />
<maximumFileSize value="1000kb" />
<staticLogFileName value="false" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
</layout>
</appender>
This functionality is part of version 1.2.11. There is a new setting for the rolling file appender:
<preserveLogFileNameExtension value="true"/>
Here is the link to the JIRA issue:
https://issues.apache.org/jira/browse/LOG4NET-64
In order to do this first you need to set your config file:
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender,log4net">
<File type="log4net.Util.PatternString" value="App_Data/%property{LogName}" />
...
</appender>
Then in your Global.asax file you set your LogName details:
private static readonly log4net.ILog log = log4net.LogManager.GetLogger("Global.asax");
void Application_Start(object sender, EventArgs e)
{
// Set logfile name and application name variables
log4net.GlobalContext.Properties["LogName"] = String.Format("{0}.2.log",DateTime.Now.ToString("yyyyMMdd"))
...
}
I am creating different reports and trying to pass the report name and append it to the name of the log4net report. The report name will be different each time the report is run so it will be dynamically assigned.
I've tried using the LOG4NET.GLOBALCONTEXT.PROPERTIES("RptName") = sReportName", but all I get is a 'null' value in the report title.
class Program
{
static void Main(string[] args)
{
log4net.Config.XmlConfigurator.Configure();
log4net.ILog log = log4net.LogManager.GetLogger(typeof(Program));
log4net.GlobalContext.**Properties("RptName")** = sReportName
log.Info("this is an info message");
Console.ReadLine();
}
}
<!--RollingFileAppender: Provides output of all log entries to a file in the \logs directory
Filename: RSGetReport_yyyyMMdd_hh:mm:ss_processID.txt-->
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file type="log4net.Util.PatternString" value="logs\RSGetReport_%date{yyyyMMdd}_%date{hhmmss}_%processid**_%property{myContext}.**txt" />
<appendToFile value="true" />
<rollingStyle value="Once" />
<maxSizeRollBackups value="10" />
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="ALL" />
</filter>
<maximumFileSize value="1MB" />
<countDirection value="1" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
</layout>
</appender>strong text
You need to set the global property before configuring log4net. See also this question.