NLog - 正确的缓冲方式
NLog - Proper way of buffering
问题
我们如何在NLog中读取日志缓冲区的内容?
详情
我在我的应用程序中配置了一个普通的 FileTarget
和一个 BufferingTargetWrapper
。每隔几分钟,我的应用程序就会运行一组步骤并在此过程中记录一些消息。我想继续缓冲这些消息并在过程结束时刷新它们。在刷新时间,我还想在电子邮件中发送 当前批次 中的所有消息。然后将缓冲区刷新到主日志文件并为下一批做好准备。
我已经以编程方式配置了我的记录器:
/// <summary>
/// Initializes global logging service
/// </summary>
private void InitLogger()
{
var config = new LoggingConfiguration();
var fileTarget = new FileTarget("MyService")
{
FileName = "${basedir}/myservice.log",
Layout = "${longdate} ${level} ${message} ${exception}"
};
config.AddTarget(fileTarget);
var bufferTarget = new BufferingTargetWrapper("BufferLog", fileTarget);
config.AddRuleForAllLevels(bufferTarget);
LogManager.Configuration = config;
}
这是一个示例测试方法,它准确地显示了我想要实现的目标:
public static Logger Log => LogManager.GetLogger("BufferLog");
[TestMethod]
public void BufferedLogTest()
{
InitLogger();
Log.Info("Message 1");
Log.Info("Message 2");
Log.Info("Message 3");
LogManager.Configuration.FindTargetByName<BufferingTargetWrapper>("BufferLog").Flush(new NLog.Common.AsyncContinuation((s) =>
{
//I want to send these 3 buffered messages through email and
//flush them to the main log file, but s is null unfortunately
}));
Log.Info("Message 4");
Log.Info("Message 5");
Log.Info("Message 6");
LogManager.Configuration.FindTargetByName<BufferingTargetWrapper>("BufferLog").Flush((s) =>
{
//I want to send next 3 buffered messages through email and
//flush them to the main log file, but s is null again
});
}
我的预期是 BufferingTargetWrapper
会将其缓冲区的内容移交给目标函数,但它没有。也没有任何其他 direct/indirect 访问缓冲区的方法。
根据@RolfKristensen 的评论(谢谢你),结果比我想象的要容易。我现在正在我的 NLog 配置中创建两个 Target
。一个继续缓冲日志消息,直到我在每批结束时手动刷新它们,而另一个继续将它们写入主日志文件。
如果这对任何人有帮助,这是我的编程方式(您也可以通过配置文件来完成):
/// <summary>
/// Initializes global logging service
/// </summary>
private void InitLogger()
{
// Create configuration object
var Config = new LoggingConfiguration();
// Create main target that continuously writes to the log file
var FileTarget = new FileTarget("FileTarget")
{
FileName = "${basedir}/myservice.log",
Layout = "${longdate} ${level} ${message} ${exception}"
};
Config.AddTarget(FileTarget);
//Create memory target that buffers log messages
var MemTarget = new MemoryTarget("MemTarget");
// Define rules
Config.AddRuleForAllLevels(MemTarget);
Config.AddRuleForAllLevels(FileTarget);
// Activate the configuration
LogManager.Configuration = Config;
}
我现在可以像这样在每个批次结束时调用提取缓冲消息:
var MemTarget = LogManager.Configuration.FindTargetByName<MemoryTarget>("MemTarget");
//Do whatever you want with buffered messages in MemTarget.Logs
MemTarget.Logs.Clear();
问题
我们如何在NLog中读取日志缓冲区的内容?
详情
我在我的应用程序中配置了一个普通的 FileTarget
和一个 BufferingTargetWrapper
。每隔几分钟,我的应用程序就会运行一组步骤并在此过程中记录一些消息。我想继续缓冲这些消息并在过程结束时刷新它们。在刷新时间,我还想在电子邮件中发送 当前批次 中的所有消息。然后将缓冲区刷新到主日志文件并为下一批做好准备。
我已经以编程方式配置了我的记录器:
/// <summary>
/// Initializes global logging service
/// </summary>
private void InitLogger()
{
var config = new LoggingConfiguration();
var fileTarget = new FileTarget("MyService")
{
FileName = "${basedir}/myservice.log",
Layout = "${longdate} ${level} ${message} ${exception}"
};
config.AddTarget(fileTarget);
var bufferTarget = new BufferingTargetWrapper("BufferLog", fileTarget);
config.AddRuleForAllLevels(bufferTarget);
LogManager.Configuration = config;
}
这是一个示例测试方法,它准确地显示了我想要实现的目标:
public static Logger Log => LogManager.GetLogger("BufferLog");
[TestMethod]
public void BufferedLogTest()
{
InitLogger();
Log.Info("Message 1");
Log.Info("Message 2");
Log.Info("Message 3");
LogManager.Configuration.FindTargetByName<BufferingTargetWrapper>("BufferLog").Flush(new NLog.Common.AsyncContinuation((s) =>
{
//I want to send these 3 buffered messages through email and
//flush them to the main log file, but s is null unfortunately
}));
Log.Info("Message 4");
Log.Info("Message 5");
Log.Info("Message 6");
LogManager.Configuration.FindTargetByName<BufferingTargetWrapper>("BufferLog").Flush((s) =>
{
//I want to send next 3 buffered messages through email and
//flush them to the main log file, but s is null again
});
}
我的预期是 BufferingTargetWrapper
会将其缓冲区的内容移交给目标函数,但它没有。也没有任何其他 direct/indirect 访问缓冲区的方法。
根据@RolfKristensen 的评论(谢谢你),结果比我想象的要容易。我现在正在我的 NLog 配置中创建两个 Target
。一个继续缓冲日志消息,直到我在每批结束时手动刷新它们,而另一个继续将它们写入主日志文件。
如果这对任何人有帮助,这是我的编程方式(您也可以通过配置文件来完成):
/// <summary>
/// Initializes global logging service
/// </summary>
private void InitLogger()
{
// Create configuration object
var Config = new LoggingConfiguration();
// Create main target that continuously writes to the log file
var FileTarget = new FileTarget("FileTarget")
{
FileName = "${basedir}/myservice.log",
Layout = "${longdate} ${level} ${message} ${exception}"
};
Config.AddTarget(FileTarget);
//Create memory target that buffers log messages
var MemTarget = new MemoryTarget("MemTarget");
// Define rules
Config.AddRuleForAllLevels(MemTarget);
Config.AddRuleForAllLevels(FileTarget);
// Activate the configuration
LogManager.Configuration = Config;
}
我现在可以像这样在每个批次结束时调用提取缓冲消息:
var MemTarget = LogManager.Configuration.FindTargetByName<MemoryTarget>("MemTarget");
//Do whatever you want with buffered messages in MemTarget.Logs
MemTarget.Logs.Clear();