log4net generated file has a wrong (null) name - log4net

I'm using log4net to log things. In my web application, the Web.config file contains the following section:
<appender name="FileAppender" type="log4net.Appender.RollingFileAppender">
<file type="log4net.Util.PatternString" value="\\PathWhereThingsAreLogged\%property{HostName}-NameOfTheApplication" />
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<appendToFile value="true" />
<maximumFileSize value="10MB"/>
<maxSizeRollBackups value="5" />
<rollingStyle value="Size" />
<rollingMode value="Size" />
<datePattern value=".yyyy-MM-dd'.log'" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date (%file:%line) %-5level - %message%newline" />
</layout>
</appender>
Logs appear in the file, so that's cool, but sadly the name of the file is incorrect. Its name is "(null)-NameOfTheApplication" instead of using the %property{HostName} declared in the file declaration.
Any help would be appreciated, thanks!

Actually, properties must be declared beforehand!
In a web application, put that in your Global.asax.cs file, in the Application_Start() method:
GlobalContext.Properties["HostName"] = Environment.MachineName;
In a windows service or a job, put it in the Program.cs in the Main(string[] args) method.

Related

How to log in Azure Webjob with Log4net?

I have a web project and a console project. I've published my web project on Azure (after having the console app attached as a Azure Webjob to the web project).
Now I want to use Log4net to log in files. It works for the web project but I can't get it to work for the Webjob...
Here is my log4net config :
<log4net>
<appender name="FileAppender" type="log4net.Appender.RollingFileAppender">
<file type="log4net.Util.PatternString" value="./_logs/[%date{yyyy-MM-dd-HH}]_webjob.log" />
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="4096KB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%newline %date [%thread] %-5level %logger - %message%newline" />
</layout>
</appender>
<logger name="myloggername">
<level value="ALL" />
<appender-ref ref="FileAppender" />
</logger>
Some guy said it would work with a absolute path but it doesn't :
<file type="log4net.Util.PatternString" value="D:/home/site/wwwroot/App_Data/Logs/AccountMaintenance/log4net_%date{yyyyMMdd}.log" />
Please try the following value instead:
<file type="log4net.Util.PatternString" value="d:\home\logfiles\mylogfile.log" />

Log4Net is creating the file in wrong folder

Following is the appender written in log4net's configuration file
<appender name="RollingFileAppenderForError" type="log4net.Appender.RollingFileAppender">
<file type="log4net.Util.PatternString" value="D:\WEB\LOGs\%date{yyyyMMdd}\"/>
<lockingModel type="log4net.Appender.FileAppender+MinimalLock"/>
<appendToFile value="true"/>
<rollingStyle value="Composite"/>
<datePattern value="lo\gs_yyyyMMdd.lo\g"/>
<maxSizeRollBackups value="50"/>
<maximumFileSize value="1MB"/>
<staticLogFileName value="false"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="[%date{yyyy-MM-dd HH:mm:ss, fff}] [%property{ServiceTxnID}] [%property{TxnRequestID}] %-5level %logger{2} %ndc - %newline Exception: %message - %exception %newline "/>
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="DEBUG" />
<levelMax value="FATAL" />
</filter>
</appender>
Here the problem is, in local and QA environment log4net is writing the log file in correct folder (New folder created everyday) but in production environment its writing the log file in wrong folder let say
In the folder for day 20130706 (YYYYMMDD) its writing the files of logs_20130706.log and logs_20130707.log. and in folder 20130707 we can see the files logs_20130707.log and logs_20130708.log.
I could not get the problem exactly where we are doing a mistake. Will be great if anyone can help me in this.
I guess the file type attribute is not re-calculated as often as the datePattern, you can try this:
<appender name="RollingFileAppenderForError" type="log4net.Appender.RollingFileAppender">
<file type="log4net.Util.PatternString" value="D:\WEB\LOGs"/>
<lockingModel type="log4net.Appender.FileAppender+MinimalLock"/>
<appendToFile value="true"/>
<rollingStyle value="Composite"/>
<datePattern value="yyyyMMdd\\\\'logs_'yyyyMMdd'.log'"/>
....
</appender>

log4net - no log file produced

I have this as my config file:
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
</configSections>
<log4net>
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="c:\\logging\\EwsSearch.log" />
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="-1" />
<maximumFileSize value="100KB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
</layout>
</appender>
</log4net>
<root>
<level value="ERROR" />
<appender-ref ref="RollingFileAppender" />
</root>
</configuration>
I then have in my C# code:
public class VsiEWSSearch
{
private static readonly ILog logger = LogManager.GetLogger(typeof(VsiEWSSearch));
public EWSResponse PdeProcessInquiry(int BusinessLineCode, int ClientCaseId, string callingApp)
{
XmlConfigurator.Configure(new System.IO.FileInfo("VSI.EWSSearch.config"));
logger.Error("This is an error");
No log file is produced even though I have adjusted permissions for the directory. What's wrong?
The root node has to be inside the log4net node. Depending on the type of configuration file you need to make some further adjustments:
If you have a standalone config file for log4net then you need to remove the configuration and configSections node. I also think that log4net is not going to find you configuration file unless you specify the full path (you can of course do that without hard-coding any path in your application).
If you are simply using app.config then you do not need further modifications, but you need to call the XmlConfigurator.Configure() method without any argument.
Note: You should call the XmlConfigurator.Configure() method only once in your application.

Configuring Appenders

I have the following situation.
There are 2 classes: ClassA, ClassB. I want them to log in different files ClassA.log, ClassB.log. For this purpose I am using the RollingFileAppender. How can I write this in a shorter way than the following:
<appender name="RollingFileAppenderA" type="log4net.Appender.RollingFileAppender">
<file value="ClassA.log" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="100KB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
</layout>
</appender>
<appender name="RollingFileAppenderB" type="log4net.Appender.RollingFileAppender">
<file value="ClassB.log" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="100KB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
</layout>
</appender>
As you can see, the only thing the appenders differ is the filename where they are writing logs. So, is there a way to define a single RollingFileAppender for the given problem?
I have worked on several projects with multiple file appenders configured just like yours. To my knowledge there is no way to define a common appender configuration and then share that among multiple appenders. It probably has something to do with how log4net interprets the XML tags into Properties on the appenders. (And I have looked closely at the ForwardingAppender, but it's not built for that).
Is there any reason other than "don't repeat yourself" (DRY) that you want to clean up the configuration? If your app.config (or web.config) is getting out of hand, you can use a separate configuration file.

log4net one file per run

I need my application to create a log file each time it runs.
My preferred format would be App.log.yyyy-MM-dd_HH-mm-ss. If that's not possible, I'd settle for App.log.yyyy-MM-dd.counter
This is my current appender configuration:
<appender name="File" type="log4net.Appender.RollingFileAppender">
<file value="App.log"/>
<rollingStyle value="Date"/>
<datePattern value=".yyyy-MM-dd_HH-mm-ss"/>
<staticLogFileName value="false"/>
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
</appender>
But it creates a random number of files based on the date and time.
I assume that the application should create only one log file every time it runs, so you do not need a rolling file appender (though my solution would apply for rolling file appenders as well):
<appender name="FileAppender" type="log4net.Appender.FileAppender">
<file type="log4net.Util.PatternString" value="c:\temp\App-%date{yyyy-MM-dd_HH-mm-ss}.log" />
<appendToFile value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%2thread] %-5level - %message%newline" />
</layout>
</appender>
(Obviously you can use other your own layout and other settings for the file appender.)
Also note that you can set your rolling style as
rollingstyle="Once"
and it will create a new file every time it is run. If staticLogFileName is set to true (e.g., logname.log) the previous logs will be set to logname.log.1, logname.log.2, etc.
The number of files kept before overwriting the oldest (say, 10) can be controlled by setting
maxSizeRollBackups="10"
Edit:
My config, which creates a datestamped log per execution (unless one exists, in which case it follows the .1 rule, looks like this:
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file type="log4net.Util.PatternString" value="Logs\MyLog-%date{dd-MM-yyyy}.log" />
<appendToFile value="false" />
<maxSizeRollBackups value="-1" /> <!--infinite-->
<staticLogFileName value="true" />
<rollingStyle value="Once" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%-5level %date [%thread] %c{1} - %m%n" />
</layout>
</appender>
Not 100% sure if I need appendToFile="false" as the docs say that's done automatically when you use rollingStyle="Once", but this makes it clearer in any case.
It's documented from apache in the log4net docs at:
https://logging.apache.org/log4net/release/config-examples.html
ctrl+f for "per program execution"
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="logfile.txt" />
<appendToFile value="false" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="-1" />
<maximumFileSize value="50GB" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
</layout>
</appender>

Resources