简体   繁体   English

如何删除/释放分配给 NLog 记录器的内存

[英]How to delete/free up memory allocated to an NLog logger

The requirement is to create one log file per job and multiple jobs can run concurrently.要求是为每个作业创建一个日志文件,并且多个作业可以同时运行。

I've implemented this with NLog by creating a new target, new logger and new rule per job.我通过为每个作业创建新目标、新记录器和新规则,使用 NLog 实现了这一点。 When I have 1000s of jobs running and completing at various times, how can I avoid memory leaks with all the extra logging resources allocated per job (logger/rule/target)?当我有 1000 多个作业在不同时间运行和完成时,如何避免内存泄漏,为每个作业(记录器/规则/目标)分配所有额外的日志记录资源? Is there a way to delete loggers?有没有办法删除记录器?

You will not have any issues with leaking Logger-object, as NLog only holds weak-references.泄漏 Logger 对象不会有任何问题,因为 NLog 只保存弱引用。 So the garbage collector is allowed to collect all NLog Logger objects not kept alive with object-references in the application.因此垃圾收集器可以收集所有未在应用程序中使用对象引用保持活动状态的 NLog Logger 对象。

But you will probably have to remove the dynamically create NLog-targets.但是您可能必须删除动态创建的 NLog 目标。 You can use this method to remove a named-target:您可以使用此方法删除命名目标:

NLog.LogManager.Configuration.RemoveTarget("myRandomTargetName");

The above method has been improved with NLog 4.5, so it can be used on the active LoggingConfiguration-object.上述方法已在 NLog 4.5 中得到改进,因此它可以用于活动的 LoggingConfiguration 对象。

See also: https://nlog-project.org/documentation/v4.5.0/html/M_NLog_Config_LoggingConfiguration_RemoveTarget.htm另见: https : //nlog-project.org/documentation/v4.5.0/html/M_NLog_Config_LoggingConfiguration_RemoveTarget.htm

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM