log4net - different log-files for assembly - c#

I have a application which is loading PlugIn-Modules with reflection.
The Application is defining a log4net-log-appender in the app.config like:
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="logs\xxx.log" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="1MB" />
<staticLogFileName value="true" />
<threshold value="DEBUG" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger{2} - %message%newline" />
</layout>
</appender>
I get the Logger in the code like:
private static readonly ILog log = LogManager.GetLogger(typeof(Indexer));
Now, I am searching a way to declare different Log-Files for each Module (assembly), which is loaded as PlugIn with reflection.
The first problem is, that the modules are using Business-Library-Classes together (they are in a assembly which is used of all modules) which also generates log-entries. This entries should also be inserted in the log-file of the module.
The second problem is, I don't know the modules at developing-time. So I cannot insert some config in the app.config.

The second problem is, i don't know the modules at developing-time. So i cannot insert some config in the app.config.
This reveals that it might not be possible in the config file. When you add a new plugin, you can add code to add a new appender for the plugin.
Hers some (code) to get you inspired:
// Setup RollingFileAppender
log4net.Appender.RollingFileAppender fileAppender = new log4net.Appender.RollingFileAppender();
fileAppender.Layout = new log4net.Layout.PatternLayout("%d [%t]%-5p %c [%x] - %m%n");
fileAppender.MaximumFileSize = "100KB";
fileAppender.MaxSizeRollBackups = 5;
fileAppender.RollingStyle = log4net.Appender.RollingFileAppender.RollingMode.Size;
fileAppender.AppendToFile = true;
fileAppender.File = fileName;
fileAppender.Name = "XXXRollingFileAppender";
log4net.Config.BasicConfigurator.Configure(fileAppender);
You need to add that it is only from the plugin-assembly you want to log, and maybe omit them from the already configures RollingFileAppender.

Related

How can I set securityContext for log4net programmatically?

I am trying to set log4net securitycontext programmatically in the global.asax, but it doesn´t work. If I put this key in the web.config it works great, but i want to take those values from the database.
My web.config that works:
<log4net>
<appender name="FileAppenderGeneral" type="log4net.Appender.RollingFileAppender">
<param name="File" value="Logs\app.log" />
<appendToFile value="true" />
<rollingStyle value="Date" />
<datePattern value="'.'dd" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%d %-5p - %m%n" />
</layout>
<<securityContext type="log4net.Util.WindowsSecurityContext">
<domainName value="hostname"/>
<userName value="username" />
<password value="password" />
</securityContext>
<encoding value="utf-8" />
</appender>
My global.asax that not works:
log4net.Config.XmlConfigurator.Configure();
ILog[] loggers = LogManager.GetCurrentLoggers();
foreach (ILog logger in loggers)
{
foreach (IAppender appender in logger.Logger.Repository.GetAppenders())
{
if (appender.GetType() == typeof(RollingFileAppender))
{
log4net.Appender.RollingFileAppender fileAppender = (RollingFileAppender)appender;
WindowsSecurityContext securityContext = new log4net.Util.WindowsSecurityContext();
securityContext.DomainName = "hostname";
securityContext.UserName = "username";
securityContext.Password = "password";
securityContext.ActivateOptions();
fileAppender.SecurityContext = securityContext;
}
}
}
Any idea what am I doing bad in the global.asax?
I would say that you need to create your own appender.
The log4net configuration file is just config for each logger that will be used in your applications. It means that you will need to 'programmatically' change security context for all your loggers. But here is a problem. You don't have all your loggers at the start of your application.
Therefore, you need to create your own appender and inherit it from the appender that you need. That's the only way to inject your own logic in log4net behavior.

Log4net use the same appender config in xml to log to multiple files

I have in my config this appender:
appender name="TC3DataLogger" type="Intel.STHI.Device.ContextLog.TC3DataLogger" >
<file value="null" />
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<appendToFile value="true" />
<rollingStyle value="Once" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="100MB" />
<datePattern value="-yyyyMMdd-HHmm" />
<layout type="log4net.Layout.PatternLayout">
<header value="Date, CS, FeedbackInput, PWM_Duty_Cycle (Cold='-' Hot='+')
"/>
<conversionPattern value="%date{yyyy-MM-dd HH:mm:ss}, %message%newline" />
</layout>
<filter type="log4net.Filter.LoggerMatchFilter">
<acceptOnMatch value="true" />
<LoggerToMatch value="Intel.STHI.Device.ContextLog.TC3DataLogger" />
</filter>
<filter type="log4net.Filter.DenyAllFilter" />
</appender>
I need to simultaneously log to multiple files. I create each log file this way:
public void CreateNewLogFile(string FileName)
{
IAppender[] appenders = LogManager.GetLogger(TC3Logger.GetType()).Logger.Repository.GetAppenders();
foreach (TC3DataLogger rfa in appenders.OfType<TC3DataLogger>())
{
rfa.File = FileName;
rfa.MaxFileSize = MaxFileSizeKB;
//sub system that the configuration for this appender has changed.
rfa.ActivateOptions();
TC3datalogRFA = rfa;
}
}
Now every time I create a new log file, all my other loggers, log to this file. How can I simultaneously log to the different files?
I tried this:
public void WriteToLog(int CSNum, string Message, string LogfileName)
{
if (!TC3LogsDictionary[CSNum].Logging) return;
TC3LogsDictionary[CSNum].TC3datalogRFA.File = TC3LogsDictionary[CSNum].LogFileName;
TC3LogsDictionary[CSNum].TC3datalogRFA.ActivateOptions();
TC3LogsDictionary[CSNum].TC3Logger.Info(CSNum, Message);
}
But I get new files every time I try to log to the files.
The ability to log to different files would be a feature of a specific appender. You seem to be using some kind of custom appender and I have no idea whether this appender has that feature. I don't think either of the standard log4net file appenders can do this. You could create your own appender by creating a class that inherits from Log4net.Appender.AppenderSkeleton there is a tutorial here that gives an example of how to do it.

Problematic N-Tier logging with log4net

I am currently developing series of web services in WCF using .NET 4.5. For my logging I have chosen log4net framework but I found that its a big problem to use it in my project design.
I have everything separated into projects like this :
DataAccess
Common
etc. etc.
ExternalServicesContracts
ExternalServices
ExternalServicesProxies
InternalService
All of the services projects contains more services.
Situation
I need my log files to look like this ServiceName_YYYY_DDMM.log.
So in the log4net config file i need to use different loggers per namespace of the web service. I can achieve it by using loggers like this :
For the first service :
private static readonly ILog Log = LogManager.GetLogger(typeof(MyFirstService));
And the same for the second service :
private static readonly ILog Log = LogManager.GetLogger(typeof(MySecondService));
and then in config file I just point to them by loggers
<logger name="ExternalServices.MyFirstService">
<appender-ref ref="FirstServiceAppender"/>
</logger>
<logger name="ExternalServices.MySecondService">
<appender-ref ref="SecondServiceAppender"/>
</logger>
And in appenders
<appender type="log4net.Appender.RollingFileAppender" name="FirstServiceAppender">
<file value="c:\temp\FirstService"/>
<rollingStyle value="Composite" />
<datePattern value="_yyyy_MM_dd.lo\g"/>
<maxSizeRollBackups value="-1"/>
<maximumFileSize value="100MB"/>
....
</appender>
<appender type="log4net.Appender.RollingFileAppender" name="SecondServiceAppender">
<file value="c:\temp\SecondService"/>
<rollingStyle value="Composite" />
<datePattern value="_yyyy_MM_dd.lo\g"/>
<maxSizeRollBackups value="-1"/>
<maximumFileSize value="100MB"/>
....
</appender>
Problem
After this change, I can`t see anything from my DataAccess and Common because I had to do the same strategy with classes inside the layer.
So the logger will be loaded the same way like this:
private static readonly ILog Log = LogManager.GetLogger(typeof(UsersProvider));
I could add another logger for both projects but I need the logs from it to be written inside the same file (the log file used by the caller).
Question
I really cant separate the services into more projects because of the client.
Is there a way to use the same logger inside a library as the caller is using ? (So the DataAccess and Common writes logs into FirstServiceAppender when the service is using it.)
Or is there any pattern that would get me out of this situation ?
I don't have time to try this out right now, but this should be easier that you think it is. You can try configuring log4net to have a dynamic file name based on a GlobalContext.Properties value. In each service, put a value GlobalContext.Properties that "names" the service. This should be ok as each service is essentially a separate process, so using the GlobalContext should not result in any conflicts. This way you will need only one file target and your loggers can be configured very simply (just send the output to the one file target).
Configuration should be something like this:
<appender type="log4net.Appender.RollingFileAppender" name="RollingFileAppender">
<file type="log4net.Util.PatternString" value="C:\temp\%property{Service}"/>
<rollingStyle value="Composite" />
<datePattern value="_yyyy_MM_dd.log"/>
<maxSizeRollBackups value="-1"/>
<maximumFileSize value="100MB"/>
....
</appender>
<root>
<appender-ref ref="RollingFileAppender" />
</root>
In your code, when each service is initialized, put something like this:
GlobalContext.Properties["Service"] = "FirstService"; // for FirstService
GlobalContext.Properties["Service"] = "SecondService"; // for SecondService
Note that you must set the value into GlobalContext.Properties BEFORE anything else is done with log4net. The best place would be in the static constructor of your application/service.
This configuration should all logs generated in "FirstService" to be written to the FirstService log file and all logs generated in "SecondService" to be written to the SecondService log.
Update:
<appender name="DynamicRollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file type="log4net.Util.PatternString" value="c:\temp\%property{Service}" />
<datePattern value="_yyyy_MM_dd.lo\g" />
<appendToFile value="true" />
<maxSizeRollBackups value="-1" />
<maximumFileSize value="5000KB" />
<preserveLogFileNameExtension value="true"/>
<staticLogFileName value="false" />
<countDirection value="1"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%method] - %message%newline" />
</layout>
</appender>
You can pass logger as a parameter for classes. So maybe your DataAccess classes should take a logger as a constructor parameter and use it for loggin. Then you could give a desired logger from outside.

Log4Net - Change Log File Folder Dynamically [duplicate]

I want to save all logs during each day in folder named YYYYMMdd - log4net should handle creating new folder depending on system datetime - how I can setup this?
I want to save all logs during the day to n files of 1MB - I don't want to rewrite old files but to really have all logs during one day - how I can setup this?
I am usin C#
Regards
Alex
Try this (It should be OK!):
<appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="logs\\" />
<appendToFile value="true" />
<DatePattern value="yyyy\\\\MM\\\\dd'.inf.log'" />
<rollingStyle value="Date" />
<param name="StaticLogFileName" value="false" />
<layout type="log4net.Layout.PatternLayout">
<header value="[Header]
" />
<footer value="[Footer]
" />
<conversionPattern value="%date [%thread] %-5level %logger [%ndc] <%property{auth}> - %message%newline" />
</layout>
</appender>
It will create a logfile named 'logs\2010\04\02.inf.log' (let date be 2010-04-02)
Thank you all.
We created SortByFolderFileAppender, which inherit from RollingFileAppender
Example of final result:
somewhere\Logs\20100305\Client-104615.0
namespace CustomLogging
{
public class SortByFolderFileAppender : log4net.Appender.RollingFileAppender
{
protected override void OpenFile(string fileName, bool append)
{
//Inject folder [yyyyMMdd] before the file name
string baseDirectory = Path.GetDirectoryName(fileName);
string fileNameOnly = Path.GetFileName(fileName);
string newDirectory = Path.Combine(baseDirectory, DateTime.Now.ToString("yyyyMMdd"));
string newFileName = Path.Combine(newDirectory, fileNameOnly);
base.OpenFile(newFileName, append);
}
}
}
<appender name="SortByFolderFileAppender" type="CustomLogging.SortByFolderFileAppender">
<file type="log4net.Util.PatternString" value="Logs\Client"/>
<appendToFile value="true"/>
<rollingStyle value="Composite"/>
<datePattern value="-HHmmss"/>
<maxSizeRollBackups value="40"/>
<maximumFileSize value="1MB"/>
<countDirection value="1"/>
<encoding value="utf-8"/>
<staticLogFileName value="false"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date{HH:mm:ss.fff}|%-5level|%message%newline"/>
</layout>
</appender>
I believe, you may create your own appender, based on the FileAppender class. You may need to override the OpenFile method to create a file in the right location.
You could use the rollinglogfileappender which creates files named by date and starts a new file at midnight. Then just write a script that moves them to a correct map at 00.01.
As for logging all files during one day with a maximum of 1 MB per file, here's an example:
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<file type="log4net.Util.PatternString" value="Logging\\MWLog"/>
<appendToFile value="true"/>
<rollingStyle value="Composite"/>
<datePattern value="-yyyyMMdd"/>
<maxSizeRollBackups value="-1"/>
<maximumFileSize value="1MB"/>
<countDirection value="1"/>
<encoding value="utf-8"/>
<staticLogFileName value="false"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date{HH:mm:ss.fff}|%-5level|%message%newline"/>
</layout>
</appender>
To build from the answer above using the SortByFolderFileAppender.
This is how we resolved the issue using rolling date for log filenames. I changed the staticLogFileName to true so the entire filename is passed into the OpenFile method.
If the filename ends in ".log" then nothing needs to be appended. I'm guessing some kind of locking has occurred and I want log4net to try using the same filename again, hoping the previous lock has been released.
Although, I'm not sure if this could end up causing an infinite call to OpenFile if the file is locked and won't let go of it. We did create a web service using the producer consumer pattern to log everything in one location from all applications in the system, which is currently ten and growing.
We don't need to include log4net into any of the other applications but we needed to create a web client class that is accessible by all applications to use for logging to the web service.
The result for the filename is "Logs\Client\2017\11\08.log" and obviously changes everyday.
protected override void OpenFile( string fileName, bool append )
{
// append "\yyyy\mm\dd.log" to create the correct filename.
if ( !fileName.EndsWith( ".log") )
fileName = $#"{fileName}\{DateTime.Now:yyyy\\MM\\dd}.log";
base.OpenFile( fileName, append );
}
Modification of the configuration from above.
<appender name="xxxRollingFileAppender" type="namespace.xxxRollingFileAppender">
<file value="Logs\Client"/>
<staticLogFileName value="true"/>
<appendToFile value="true"/>
<maxSizeRollBackups value="40"/>
<maximumFileSize value="1MB"/>
<encoding value="utf-8"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date{HH:mm:ss.fff}|%-5level|%message%newline"/>
</layout>
</appender>

Creating new Log file everyday and deleting previous log files

I have one windows service. For Logging purpose in that service I am using Log4Net.dll. Now my requirement is that I want to create a new log file everyday and it should keep the log of only previous 7 days. Means on 8th day, it should delete first day file and use new file. I am using appender as:
<appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender">
<file type="log4net.Util.PatternString" value="D:\Log\%property{LogName}" />
<AppendToFile value="true" />
<rollingStyle value="Date" />
<maxSizeRollBackups value="100" />
<staticLogFileName value="false" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%newline %date %-5level %C.%M() - %message" />
</layout>
</appender>
And at service startup I am configuring logger as:
log4net.GlobalContext.Properties["LogName"] = "App_" + DateTime.Now.ToString("MMddyyyy") + ".log";
log4net.Config.XmlConfigurator.Configure();
So is there any functionality of Log4Net that will achieve above functionality? Or is there any other way to do it? Any type of help will be appreciated.
I'm afraid you can't: RollingFileAppender Class
A maximum number of backup files when rolling on date/time boundaries
is not supported
Have a look at this thread for other suggestions: Log4Net: set Max backup files on RollingFileAppender with rolling Date
To implement log file everyday you would use:
<rollingStyle value="Date" />
<datePattern value="yyyyMMdd" />

Categories