"Manually" rolling a log4net RollingFileAppender log file? - log4net

Rather than roll a log after a date/time or specified maximum size, I want to be able to call a method like "ResetLog" that copies my "log.txt" to "log.txt.1" and then clears log.txt.
I've tried to implement that by doing something like this with a FileAppender, rather than a RollingFileAppender:
var appenders = log4net.LogManager.GetRepository().GetAppenders();
foreach (var appender in appenders) {
var fa = appender as log4net.Appender.FileAppender;
if (fa != null) {
string logfile = fa.File;
fa.Close();
string new_file_path = CreateNextLogFile(logfile);
fa.File = new_file_path;
fa.ActivateOptions();
}
}
The file is closed and CreateNextLogFile() renames it. I then create a new log file and set the FileAppender to use it. However, I thought that ActivateOptions would go ahead and reconfigure the FileAppender with the desired settings. I've looked over the log4net documentation and don't see any other public methods that allow me to reopen the FileAppender after closing it. Can anyone recommend a way to implement the rollover? It would be nice if the RollingFileAppender had something like this, but I didn't see anything useful it its documentation, either.

If we look at the RollingFileAppender we can see that the mechanism for rolling over consists of closing the file, renaming existing files (optional) and opening it again:
// log4net.Appender.RollingFileAppender
protected void RollOverSize()
{
base.CloseFile();
// debug info removed
this.RollOverRenameFiles(this.File);
if (!this.m_staticLogFileName && this.m_countDirection >= 0)
{
this.m_curSizeRollBackups++;
}
this.SafeOpenFile(this.m_baseFileName, false);
}
Unfortunately the CloseFile/SafeOpenFile methods are protected which means you cannot access it from the outside (not easily). So your best bet would be to write an appender inheriting from RollingFileAppender, and to override the virtual AdjustFileBeforeAppend which is called before any logging event is added to the appender.
There you can decide what are the conditions of the roll if any must occur. An idea would be to create a static event that your custom rolling appender suscribes to: when the event is triggered the appender makes a note of it (rollBeforeNextAppend = true;). As soon as you try and log next entry the appender will roll.
public class CustomRollingAppender: RollingFileAppender
{
public CustomRollingAppender()
{
MyStaticCommandCenter.RollEvent += Roll;
}
public void Roll()
{
rollBeforeNextAppend = true;
}
public bool rollBeforeNextAppend {get; set;}
public override void AdjustFileBeforeAppend()
{
if (rollBeforeNextAppend) {
CloseFile();
RollOverRenameFiles(File);
SafeOpenFile(Filename, false);
rollBeforeNextAppend = false;
}
}
}

Related

How to migrate Log4j Filter to Log4j 2

I got a Java Webapplication that starts an asynchron server side "job".
The application creates a directory for each job and logs in a file in this directory.
My implementation with log4j is:
import org.apache.log4j.*;
public class ThreadLogger {
String sThreadName;
String sLogfilePath;
RollingFileAppender rfaJob;
PatternLayout plJobLog;
public ThreadLogger(){}
public void start(String sThreadId, String sLogfilePath){
this.sThreadName = sThreadId;
this.sLogfilePath = sLogfilePath;
// Create Logfilter and LogAppender for thread based logging
ThreadLoggingFilter ThreadLogFilter = new ThreadLoggingFilter(this.sThreadName);
plJobLog = new PatternLayout("[%x - %t][%d / %p / %c] - %m%n");
this.rfaJob = new RollingFileAppender();
this.rfaJob.setLayout(plJobLog);
this.rfaJob.setFile(sLogfilePath);
this.rfaJob.setEncoding("UTF-8");
this.rfaJob.activateOptions();
this.rfaJob.setMaxBackupIndex(9);
this.rfaJob.setMaxFileSize("10MB");
this.rfaJob.setThreshold(Level.ALL);
this.rfaJob.addFilter(ThreadLogFilter);
Logger.getRootLogger().addAppender(this.rfaJob);
}
public void stop(){
Logger.getRootLogger().removeAppender(this.rfaJob);
this.rfaJob.close();
}
}
and the ThreadLogginFilter is:
import org.apache.log4j.spi.*;
public class ThreadLoggingFilter extends Filter {
String threadName;
public ThreadLoggingFilter(String _threadName){
this.threadName = _threadName;
}
#Override
public int decide(final LoggingEvent event) {
if (event.getNDC() != null && event.getNDC().equals(this.threadName)) {
return ACCEPT;
}
return DENY;
}
}
No I want to implement this with log4j2 and donĀ“t know how to do the filter.
I know the documentation on https://logging.apache.org/log4j/2.0/manual/filters.html but I cant find a way to do this.
Is it possible to do this without a configuration?
Update: The goal...
My webapplication starts threads. Every thread produces several files to a folder that will be send to the user at the end. Within the folder there has to be the log file. So every thread need his own appender with a foldername.

Serilog Seq Sink not always capturing events

I'm running Seq on an Azure instance (Windows Server 2012 R2 Datacenter) and logging with Serilog from a console application running on my local workstation. I have 3 sinks configured - File, Console and Seq. I'm also running on dnxcore50 (just in case you were thinking my setup wasn't dodgy enough). All my events are showing up in console and the file 100% of the time. Seq is only capturing event about 1 in 5 or more runs, that is it will either capture all the events for the run or none of them. I am using the free single user license to evaluate Seq, and I haven't found anything to suggest there are any limitations that would cause this behavior.
I've set up SelfLog.Out on the loggers, but that logs nothing at all, other than my test line which I added to make sure the self logging could at least write to the specified file.
public abstract class SerilogBaseLogger<T> : SerilogTarget
{
protected SerilogBaseLogger(SerilogOptions options, string loggerType)
{
LoggerConfiguration = new LoggerConfiguration();
Options = options;
LevelSwitch.MinimumLevel = options.LogLevel.ToSerilogLevel();
var file = File.CreateText(Path.Combine(options.LogFilePath, string.Format("SelfLog - {0}.txt", loggerType)));
file.AutoFlush = true;
SelfLog.Out = file;
SelfLog.WriteLine("Testing self log.");
}
// ... snip ....
}
public class SerilogSeqTarget<T> : SerilogBaseLogger<T>
{
public string Server => Options.Seq.Server;
public SerilogSeqTarget(SerilogOptions options)
: base(options, string.Format("SerilogSeqTarget[{0}]", typeof(T)))
{
LoggerConfiguration
.WriteTo
.Seq(Server);
InitializeLogger();
}
public override string ToString()
{
return string.Format("SerilogSeqTarget<{0}>", typeof(T).Name);
}
}
public class SerilogLoggerFactory<TType> : LoggerFactory<SerilogTarget>
{
// .... snip ....
public override IMyLoggerInterface GetLogger()
{
var myLogger = new SerilogDefaultLogger()
.AddTarget(SerilogTargetFactory<TType>.GetFileTarget(Options))
.AddTarget(SerilogTargetFactory<TType>.GetConsoleTarget(Options))
.AddTarget(SerilogTargetFactory<TType>.GetSeqTarget(Options));
myLogger.Info("Logger initialized with {#options} and targets: {targets}", Options, ((SerilogDefaultLogger)myLogger).Targets);
return myLogger;
}
}
public class SerilogTargetFactory<TType>
{
// ... snip ...
public static SerilogTarget GetSeqTarget(SerilogOptions options)
{
return !string.IsNullOrEmpty(options.Seq.Server)
? new SerilogSeqTarget<TType>(options)
: null;
}
}
Any suggestions? Is this just a side-effect of being on the bleeding edge, working with pre-release everything (although in that case I'd expect things to fail somewhat consistently)?
when targeting dnxcore50/CoreCLR, Serilog can't hook into AppDomain shutdown to guarantee that any buffered messages are always flushed. (AppDomain doesn't exist in that profile :-)
There are a couple of options:
Dispose the loggers (especially the Seq one) on shutdown:
(logger as IDisposable)?.Dispose();
Use the static Log class and call its CloseAndFlush() method on shutdown:
Log.CloseAndFlush();
The latter - using the static Log class instead of the various individual ILogger instances - is probably the quickest and easiest to get going with, but it has exactly the same effect as disposing the loggers so either approach should do it.

Create multiple Logfiles with dynamic Names with log4net

I'm using log4net in a Windows Service. This Service processes some RFID Reader. Currently we are logging all tasks of all Reader in one Logfile. This works fine.
But now I want to log the tasks of each Reader in a separate File. The Readers are identified by their IP Address. So I want to take the IP Address as part of the Filename.
The option in log4net to create dynamic file appenders seems not to fit for me, because I would have to manage the assignment from Reader to log file, each time I write a log.
Is there an appropriate way to do this in log4net, or is it not possible?
In my Logclass I used a Dictionary<string, ILog> for my Loggers. I've overloaded methods, either they use the Default-Logger or they get the Key for the Dictionary to use the specified Logger.
public static class Log
{
private static readonly Dictionary<string, ILog> loggers = new Dictionary<string, ILog>();
static Log()
{
XmlConfigurator.Configure();
}
public static void Debug(string message)
{
Debug(Logger.Default, message);
}
public static void Debug(string readerIp, string message)
{
GetLoggerInternal(readerIp).Debug(message);
}
private static ILog GetLoggerInternal(string logger)
{
if (!loggers.ContainsKey(logger))
{
var appender = CreateRollingFileAppender(logger);
appender.ActivateOptions();
loggers.Add(logger, LogManager.GetLogger(logger));
((log4net.Repository.Hierarchy.Logger)loggers[logger].Logger).AddAppender(appender);
}
return loggers[logger];
}
private static RollingFileAppender CreateRollingFileAppender(string readingPointIp)
{
var layout = new PatternLayout
{
ConversionPattern = "%date [%thread] %-5level %logger [%property{NDC}] - %message%newline"
};
layout.ActivateOptions();
return new RollingFileAppender
{
Name = readingPointIp,
AppendToFile = true,
DatePattern = "yyyyMMdd",
MaximumFileSize = "1MB",
MaxSizeRollBackups = 10,
RollingStyle = RollingFileAppender.RollingMode.Composite,
File = $"..\\Log\\{readingPointIp}_log.txt",
Layout = layout
};
}
}
It is important to call the .ActivateOptions(); methods, they instantiate the Appender and Layout Classes. I use LogManager.GetLogger to create a new Logger. To add the appender I've to cast the logger, to use AddAppender.
Now I just have to call Log.Debug(readingPoint.IpAddress, "Some readingpoint specific log message."); and I've this message in a file, with the IP Address in it's name.

NLog MemoryTarget maximum size

Since I have a global exception handler that reports uncaught errors via e-mail, next step is to add some context to it by having some 10-20 last lines of log that are collected.
So I am using MemoryTarget like so:
MemoryTarget _logTarget;
_logTarget = new MemoryTarget();
_logTarget.Layout = "${longdate}|${level:uppercase=true}|${logger}|${message}${exception}";
LoggingRule loggingRule = new LoggingRule("*", LogLevel.Debug, _logTarget);
LogManager.Configuration.AddTarget("exceptionMemory", _logTarget);
LogManager.Configuration.LoggingRules.Add(loggingRule);
LogManager.Configuration.Reload();
Apps containing this should run forever, and if I leave logs in memory, unchecked, I'll have neatly designed memory leak.
How to address this? How to truncate MemoryTarget.Logs to have at most say 100 lines?
Your best bet is probably to write your own MemoryTarget... Something like this (untested) should work.
namespace NLog.Targets
{
using System.Collections.Generic;
[Target("LimitedMemory")]
public sealed class LimitedMemoryTarget : TargetWithLayout
{
private Queue<string> logs = new Queue<string>();
public LimitedMemoryTarget()
{
this.Logs = new List<string>();
}
public IEnumerable<string> Logs
{
get { return logs; }
private set { logs = value; }
}
[DefaultValue(100)]
public int Limit { get; set; }
protected override void Write(LogEventInfo logEvent)
{
string msg = this.Layout.Render(logEvent);
logs.Enqueue(msg);
if (logs.Count > Limit)
{
logs.Dequeue();
}
}
}
}
This example is based on the NLog MemoryTarget, the source code for which you can find here:
https://github.com/NLog/NLog
NLog docs are here:
http://nlog-project.org/documentation/v2.0.1/
I didn't see anything like you are asking about in either location.

LOG4J Load muliple log4j files

I have five different log4j property files in my application for each event. I do not want the application to load the file using DOM or PropertyConfigurator. But I want to load all these properties into a Map with event name as key and Properties or Logger as the value. So that when I invoke the getLogger method with the event name the appropriate logger object based on the event name will be returned.
The implementation in this post helps me to some extent. log4j log file names?
They are dynamically creating logger objects based on the job. But I want to make use of the static log4j file for each event and load it and give it back.
I also checked the response in this post. multiple log4j instance configuration
But as the event names and the list of appenders for each event will be a huge number in my application, for better maintainability purposes, I am opting for one log4j file for each event.
Expecting your help on this.
Thanks,
Radhika
I have log4j files for each event defined.
Pass the event name and using property configurator load the event specific log4j property file in context. The getLogger method then will have that Logger.
private static synchronized Logger getEventLogger(String eventName) {
Logger logger = null;
try {
logger = m_loggers.get(eventName);
if (logger == null) {
PropertyConfigurator.configure(eventName + ".properties");
logger = Logger.getLogger(eventName);
m_loggers.put(eventName, logger);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return logger;
}

Resources