Where will log4net create this log file? - c#

When I set the file value to logs\log-file.txt, where exactly will it create this folder? In the /bin directory?
My web.config looks like this:
<log4net>
<appender name="FileAppender" type="log4net.Appender.FileAppender">
<file value="logs\log-file.txt" />
<appendToFile value="true" />
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
</layout>
</appender>
</log4net>
Is this the correct way to log:
ILog logger = LogManager.GetLogger(typeof(CCController));
logger.Error("Some Page", ex); // where ex is the exception instance

If you want your log file to be place at a specified location which will be decided at run time may be your project output directory then you can configure your .config file entry in that way
<file type="log4net.Util.PatternString" value="%property{LogFileName}.txt" />
and then in the code before calling log4net configure, set the new path like below
log4net.GlobalContext.Properties["LogFileName"] = #"E:\\file1"; //log file path
log4net.Config.XmlConfigurator.Configure();
How simple is it? :)

it will create the file in the root directory of your project/solution.
You can specify a location of choice in the web.config of your app as follows:
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="c:/ServiceLogs/Olympus.Core.log" />
<appendToFile value="true" />
<rollingStyle value="Date" />
<datePattern value=".yyyyMMdd.log" />
<maximumFileSize value="5MB" />
<staticLogFileName value="true" />
<lockingModel type="log4net.Appender.RollingFileAppender+MinimalLock" />
<maxSizeRollBackups value="-1" />
<countDirection value="1" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %-5level [%thread] %logger - %message%newline%exception" />
</layout>
</appender>
the file tag specifies the location.

The file value can either be an absolute path like "c:\logs\log.txt" or a relative path which I believe is relative to the bin directory.
As far as implementing it, I usually place the following at the top of any class I plan to log in:
private static readonly ILog Log = LogManager.GetLogger(
MethodBase.GetCurrentMethod().DeclaringType);
Finally, you can use it like so:
Log.Debug("This is a DEBUG level message.");

Log4net is saving into your project folder. Something like: \SolutionFolder\ProjectFolder\bin\SolutionConfiguration\logs\log-file.txt.
Where:
SolutionFolder is where you save your solution
ProjectFolder is the folder where your project lives into the solution and
SolutionConfiguration is the folder that contais all the binaries of your project (the default is Debug or Release)
Hope this helps!

FileAppender appender = repository.GetAppenders().OfType<FileAppender>().FirstOrDefault();
if (appender != null)
logger.DebugFormat("log file located at : {0}", appender.File);
else
logger.Error("Could not locate fileAppender");

For the log folder and file stuff, go with #Bens answer.
I will comment on the creating log part, though. Imo there is no correct way. When coding loggers manually I do it the way you're doing it:
ILog logger = LogManager.GetLogger(typeof(CCController));
because it is short and concise.
That said, I do not create the logger instances inside the classes these days, I let my IoC container inject it for me.

I was developing for .NET core 2.1 using log4net 2.0.8 and found NealWalters code moans about 0 arguments for XmlConfigurator.Configure(). I found a solution by Matt Watson here
log4net.GlobalContext.Properties["LogFileName"] = #"E:\\file1"; //log file path
var logRepository = LogManager.GetRepository(Assembly.GetEntryAssembly());
XmlConfigurator.Configure(logRepository, new FileInfo("log4net.config"));

I think your sample is saving to your project folders and unless the default iis, or .NET , user has create permission then it won't be able to create the logs folder.
I'd create the logs folder first and allow the iis user full permission and see if the log file is being created.

if you want to choose dynamically the path to the log file use the method written in this link: method to dynamic choose the log file path.
if you want you can set the path to where your app EXE file exists like this -
var logFileLocation = System.IO.Path.GetDirectoryName
(System.Reflection.Assembly.GetEntryAssembly().Location);
and then send this 'logFileLocation' to the method written in the link above like this:
Initialize(logFileLocation);
and you are ready to go! :)

In your case, the log file will be in bin\Debug\netcoreapp3.1\ folder
and this may depends on your framework too.
I used Asp.Net core 3.1 so the folder path is bin\Debug\netcoreapp3.1\

Related

Why am I getting two log files per day using log4net with ASMX web service?

I have a ASMX web service running IIS 10 and have added log4net to the project with the following configuration:
<appender name="AsmxDebugLogFile" type="log4net.Appender.RollingFileAppender">
<file value="App_Data/ASMX.DEBUG_" type="log4net.Util.PatternString" />
<appendToFile value="true" />
<rollingStyle value="Composite" />
<datePattern value="yyyy-MM-dd'.log'" />
<maximumFileSize value="5GB" />
<staticLogFileName value="false" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
</layout>
</appender>
<logger name="AsmxDebugLogFile">
<level value="DEBUG" />
<appender-ref ref="AsmxDebugLogFile" />
</logger>
But for some reason, I'm getting two log files per day in the format of yyyy-MM-dd.log.yyyy-MM-dd.log as per the screenshot below, seemingly at random times as well, whereby the log file will switch to the newly created log file, and then will start logging again in the log file at was created at midnight of the day in question. All log files are way below the 5GB max file size as well.
Below is an updated list of log files
It seems that initially, log4net logs to the file yyyy-MM-dd.log, and then after a random amount of time creates a new log file yyyy-MM-dd.log.yyyy-MM-dd.log starts writing to this log file and then goes back and logs to yyyy-MM-dd.log which was created at the start of the day.
These are the IIS recycle settings
I am wanting 1 log file per day in the format of ASMX.DEBUG_yyyy-MM-dd.log. Where am I going wrong with this configuration?
UPDATE
It seems that because we are recycling the application pool every 60 minutes depending on whether the log file is being written to at this time, depends on whether a new one is created. Any suggestions as to how to overcome this issue. At the minute I've now added the process_id in the log file name and it seems to work in that I get a new log file created every hour, but ideally I want just 1 log file a day.
I use log4net for all my applications and I never encountered this re-creation of file after a pool recycling.
Here are my parameter that differ from yours :
<appendToFile value="true"/>
<staticLogFileName value="true" />
<lockingModel type="log4net.Appender.FileAppender+MinimalLock"/>
And here's how the behaviour will be different :
staticLogFileName means current day's file name will always be the same (ASMX.DEBUG_.log here). It will be automatically renamed with the date pattern on the day after.
appendToFile means it will not overwrite current day's file, but rather append to it.
I think those first two parameters will avoid the creation of multiple file for the current day which may fix your problem.
MinimalLock's role is less clear to me, but I should mention it anyway :
Opens the file once for each AcquireLock()/ReleaseLock() cycle, thus holding the lock for the minimal amount of time. This method of locking is considerably slower than FileAppender. ExclusiveLock but allows other processes to move/delete the log file whilst logging continues.
<appender name="AsmxDebugLogFile" type="log4net.Appender.RollingFileAppender">
<lockingModel type="log4net.Appender.FileAppender+MinimalLock"/>
I think you need Locking model which will not lock your file.I think when main file gets created and get locked. The File appender creates file name with same date time stamp over the exisiting file.
You should also use Remote path for logging like \\server\file\debug since you might have load balancer for IIS or Web server is hosted with static IP or F5.
Try with this parameters.
<param name="File" value="C:\YourLogFilePath\" />
<rollingStyle value="Composite" />
<param name="DatePattern" value="dd.MM.yyyy'.log'" />
<param name="AppendToFile" value="true" />
<maxSizeRollBackups value="2" />
<maximumFileSize value="5GB" />
<staticLogFileName value="false" />
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
You are using the "RollingFileAppender" which as its name suggests rolls to a new file on dates, try using the FileAppender, more information about how to use FileAppender you can refer to this link: FileAppender.

Log4net sometimes creating two logs, one empty

Using the below configuration, about half of the time, I'm getting two logs per one, one empty. They differ in filename by one second (log.2015-03-09_11-50-25 vs log.2015-03-09_11-50-26)
I'm trying to have one log per run of the console application.
<log4net>
<appender name="Log" type="log4net.Appender.FileAppender">
<file type="log4net.Util.PatternString" value="C:\env\QA\Logs\consoleapp\log.%date{yyyy-MM-dd_HH-mm-ss}.txt"/>
<appendToFile value="true"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %message%newline"/>
</layout>
</appender>
<root>
<level value="INFO"/>
<appender-ref ref="Log"/>
</root>
</log4net>
The latter log is the only one that's populated/written to.
Why is this happening? How do I fix it?
EDIT: Turns out i was instantiating a second logger in my code. When the instantiation occurred during a different second, a second log was created. The appender was working correctly.
I've seen something similar before - it's due to the second being specified in the filename, and the writes are occuring during the end of one second and the start of another.
<file type="log4net.Util.PatternString" value="C:\env\QA\Logs\consoleapp\log.%date{yyyy-MM-dd_HH-mm-ss}.txt"/>
should be
<file type="log4net.Util.PatternString" value="C:\env\QA\Logs\consoleapp\log.%date{yyyy-MM-dd}.txt"/>
In my last job, somebody set the second specifier in the log file name, and the production server came close to crashing trying the render the 150,000 files contained in the log folder.
Edit:
If you want to write once per run, you can add the following to your log4net configuration:
<rollingStyle value="Once" />
and you might need to set the appendToFile attribute to be false.
log4net one file per run
so your config would look like this:
<appender name="Log" type="log4net.Appender.FileAppender">
<file type="log4net.Util.PatternString" value="C:\env\QA\Logs\consoleapp\log.%date{yyyy-MM-dd_HH-mm-ss}.txt"/>
<appendToFile value="false" />
<maxSizeRollBackups value="-1" /> <!--infinite-->
<rollingStyle value="Once" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %message%newline"/>
</layout>
</appender>
I used user1666620's solution, but still got a .log.1 file.
Turns out I had the following line in my AssemblyInfo.cs file:
[assembly: log4net.Config.XmlConfigurator(Watch = true)]
Removing the above line fixed my issue. By the way, my program.cs looks like:
public static class Program
{
static Program()
{
// Initialize logging
log4net.Config.XmlConfigurator.Configure();
}
private static readonly ILog log =
LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
...
}

Retrieving file value from log4net's config file

I'm stuck in a situation where I believe I need to know the name of the log file that the user has specified in logging.xml. Specifically, I am trying to retrieve the value of the value attribute in the file element, as shown below (taken from the log4net docs)
For full details see the SDK Reference entry:
log4net.Appender.FileAppender.
The following example shows how to configure the FileAppender to write messages to a file. The file specified is log-file.txt. The file
will be appended to rather than overwritten each time the logging
process starts.
<appender name="FileAppender" type="log4net.Appender.FileAppender">
<file value="log-file.txt" />
<appendToFile value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
</layout>
</appender>
When I loop over the appenders with the following code:
log4net.Repository.ILoggerRepository repo = LogManager.GetRepository();
foreach (log4net.Appender.IAppender appender in repo.GetAppenders())
{
FileAppender fa = appender as FileAppender;
}
I am unable to find the value "log-file.txt" anywhere in the fa object. Perhaps I have missed it since there are a lot of members and data exposed in the debugger.
Can anyone suggest a way to get at this value?
FileAppender has property File which contains path to the file that logging will be written to. But before you start looking for this property, verify Name of appender - you can have several FileAppenders configured.
Also keep in mind that RollingFileAppender also matches condition appender is FileAppender, because it is derived from FileAppender.
string file = repo.GetAppenders()
.OfType<FileAppender>()
.Where(fa => fa.Name == "FileAppender")
.Single()
.Name;

Setting up Log4Net to log output from a Class Library

I'm trying to setup Log4Net (this is my first time using Log4Net) to log to a text file in an assembly. I'm not getting any errors, but it's also not working. I can breakpoint the lines where I am logging my output and see that they are reached, but like I say nothing happens.
Where am I going wrong?
I have added the following to my packages.config file, inside the <packages> attribute:
<log4net>
<appender name="FileAppender" type="log4net.Appender.FileAppender,log4net">
<file value="c:\CTI\log.txt" />
<appendToFile value="true" />
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %level %logger - %message%newline" />
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="INFO" />
<levelMax value="FATAL" />
</filter>
</appender>
<root>
<level value="DEBUG"/>
<appender-ref ref="FileAppender"/>
</root>
</log4net>
</configuration>
I have added the following line to AssemblyInfo.cs:
[assembly: log4net.Config.XmlConfigurator(Watch=true)]
I added the Log4Net assembly using NuGet and I am logging like this:
private log4net.ILog _Log;
_Log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
_Log.Debug("FooBar");
Like I say, no errors but nothing happens either.
What am I missing?
One thing that is wrong is that you are adding the log4net configuration section to the nuget config file (packages.config).
You can have the configuration in the app/web config or in a separate file to which you point from the appSettings, e.g.
configuration is in a file called config.log4net (the copy to output directory attribute of the file is set to copy always) and you add the following entry in app/web.config:
<add key="log4net.config" value="config.log4net"/>
If you don't want to depend on a web/app configuration, you can set the ConfigFileExtension property of the XmlConfiguratorAttribute attribute in AssemblyInfo.cs:
[assembly: log4net.Config.XmlConfigurator(ConfigFileExtension = "log4net", Watch = true)]
Then name the log4net configuration file the same as your exe/assembly plus the configured extension, e.g. MyApplication.exe.log4net or MyLibrary.dll.log4net
Another thing that is wrong is your appender filter. The range that you have set excludes DEBUG level, which you expect to log. Here are all logging levels:
ALL
DEBUG
INFO
WARN
ERROR
FATAL
OFF
As you can see, DEBUG is not between INFO and FATAL.

log4net EventLog custom logname created, but not used

In a project, i'm using log4net to write down in the event log. I need to create a custom log so all will go there. I have tried to add the event to different logs, and it worked, but for some reasons, now it is impossible to change the logname from the config file. Even if I change it, it will keep the previous one and log into it. Here is a sample of my app.config:
<appender name="EventLogAppender" type="log4net.Appender.EventLogAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date{yyyy-MMMM-dd HH:mm:ss, fff} [%thread] %level %message %exception%newline"/>
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="DEBUG" />
<levelMax value="FATAL" />
</filter>
<ApplicationName>TestLoggerFichierConfig</ApplicationName>
<LogName>AgentLogger</LogName>
</appender>
I read that the event log must be restarted when adding a new log so the recent events will be displayed in it. I did this, and it allowed me to see the new log that has been created, but nothing will go there. Is it possible to force Log4Net to stop using the previous log to rather use the one I defined in the app.config?
Thanks for the help.
Update:
It seems that le EventLogs recognizes the source application and decide in wich log to put the new events since he remembers where it previously went. When I first set the logName, it works fine. If I stop the app, change the logname and restart it, the event will still go to the previous log. If the logName does not exists in the event log, it will be created, but not used! There might be something to do, but it is not in the side of log4net, and it may be dangerous to change the windows settings at this level. I created two eventLogAppender on the same app.config file, both pointing at different logs. the events got to the same log though. It is being obvious that the problem doesn't come from log4net, and the solution to my problem will not be solved by code. Thanks for the great advices though.
Something is probably wrong with your configuration and it is simply not telling you about it because log4net itself is designed to never, ever throw errors.
You can turn on internal debugging for log4net, but don't deploy to production with log4net debugging turned on. Weirdness will eventually happen if deploy your product with the log4net debugging switch on.
http://haacked.com/archive/2006/09/27/Log4Net_Troubleshooting.aspx
http://logging.apache.org/log4net/release/faq.html (see the troubleshooting section)
I ran into this and it wasn't down to log4net but the re-mapping of the source to a new custom log. I took log4net out of the equation and finding that the problem occurred usint the EventLog class directly, I eventually found EventLog.CreateEventSource is not creating a custom log
I think it's also the reason behind this Windows service always writes to custom log and application log
First and foremost, I'd encourage you to turn on internal debugging, as found in the link ntcolonel posted. Add this to your config file:
<add key="log4net.Internal.Debug" value="true"/>
Perhaps I am not understanding your question entirely, but I'd like to ensure you have both created the appender and properly tell log4net that you want to actually use that appender:
<log4net debug="true">
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="Logs\SomeApplication.xml"/>
<appendToFile value="true"/>
<rollingStyle value="Size"/>
<countDirection value="1"/>
<maxSizeRollBackups value="30"/>
<maximumFileSize value="10MB"/>
<staticLogFileName value="true"/>
<lockingModel type="log4net.Appender.FileAppender+MinimalLock"/>
<layout type="log4net.Layout.XmlLayoutSchemaLog4j">
<locationInfo value="true"/>
</layout>
</appender>
<appender name="SmtpAppender" type="log4net.Appender.SmtpAppender">
<to value="SomeDistributionList#somehost.com"/>
<from value="SomeApplication#somehost.com"/>
<subject type="log4net.Util.PatternString" value="Some Application Error - %property{log4net:HostName}"/>
<smtpHost value="smtp.somehost.com"/>
<bufferSize value="1"/>
<threshold value="ERROR"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %property{log4net:HostName} %logger %level %newline%newline%property{ExceptionThrottleInformation}%newline%newline%message%newline"/>
</layout>
<filter type="SomeNamespace.SomeSubNamespace.Log4Net.ExceptionThrottleFilter, SomeSubNamespace">
<threshold value="10"/>
<thresholdTimeoutSeconds value="60"/>
<timeoutSecondsBetweenMessages value="600000"/>
<exceptionText value="Timeout expired"/>
</filter>
</appender>
<appender name="DatabaseAppender" type="SomeNamespace.SomeSubNamespace.DatabaseTraceAppender, SomeSubNamespace">
<hoursToKeepInformationalTraces value="48"/>
<hoursToKeepErrorTraces value="96"/>
<threshold value="INFO"/>
</appender>
<root>
<level value="INFO"/>
<appender-ref ref="RollingFileAppender"/>
<appender-ref ref="SmtpAppender"/>
<appender-ref ref="DatabaseAppender"/>
</root>
Note that while I have multiple appenders, I reference which to call within the root tag. Since you didn't post your whole config file, it's tough to tell if everything is matching as it should.

Categories