简体   繁体   English

成功通过电子邮件发送后删除Nlog日志文件

[英]Delete Nlog log file after it was successfully emailed

I want to delete my Nlog logfile between each application run only if it was emailed to me successfully otherwise it should keep adding to the logfile. 我想仅在成功通过电子邮件发送给我的情况下删除每个应用程序运行之间的Nlog日志文件,否则应继续添加到日志文件中。 I am using Outlook to email the file. 我正在使用Outlook通过电子邮件发送文件。 I don't want to use Smtp since some networks block port 25 and then it does not get emailed to me. 我不想使用Smtp,因为某些网络会阻止端口25,然后它不会通过电子邮件发送给我。

The problem is when I try to delete the logfile with File.Delete(logfile) is says that the the file is in use by aonther process. 问题是当我尝试使用File.Delete(logfile)删除日志文件时,该文件被其他进程使用。 How do I unlock or close the file in order for me to email it using Outlook (and then re-open it fo further logging)? 如何解锁或关闭文件,以便使用Outlook通过电子邮件发送文件(然后为以后的日志记录重新打开文件)?

I was thinking of making a copy of the logfile and emailing that, but I'm not sure if its the best way to do it. 我当时想制作一个日志文件的副本并通过电子邮件发送,但是我不确定这是否是最好的方法。

Thx for any ideas. 感谢任何想法。

It's not Outlook which prevents the deletion of the file - your application is still running and logging to that file is still active, hence it is the Nlog part of your application which prevents the deletion. 不是Outlook阻止了该文件的删除-您的应用程序仍在运行,并且对该文件的日志记录仍处于活动状态,因此阻止您删除该文件的是应用程序的Nlog部分。 Tell nlog to use a different log file, or not to log at all (you may resume logging later on). 告诉nlog使用其他日志文件,或者根本不进行日志记录(以后可以继续进行日志记录)。

By default nlog doesn't keep files open (file target, keepFileOpen). 默认情况下,nlog不会保持文件打开(文件目标,keepFileOpen)。 So either you try to delete file when your application is writing data, or outlook still using the file. 因此,要么在应用程序写入数据时尝试删除文件,要么Outlook仍在使用该文件。

First, you may want to send it via outlook a copy of the log file. 首先,您可能希望通过Outlook将日志文件的副本发送给它。 So you'd be sure that original file is not locked by an external process. 因此,您将确保原始文件没有被外部进程锁定。

Second, you'll be able to reconfigure current file target to write to another file ( log(n+1).txt or something. There are some hints about programmatic configuration at Add, enable and disable NLog loggers programmatically ). 其次,您将能够重新配置当前文件目标以写入另一个文件( log(n+1).txt或其他内容。在添加,以编程方式启用和禁用NLog记录器上有一些关于编程配置的提示)。 So you'll be sure that application isn't logging to the file. 因此,您将确保应用程序未记录到文件中。

Then you'll be able to remove it, I think. 我想,您就可以删除它。

Option 1 : 选项1 :

if(chkLogger.Checked){
    NLog.Config.SimpleConfigurator.ConfigureForFileLogging("Logfile.log",NLog.LogLevel.Trace);
}
else
{
 NLog.Config.SimpleConfigurator.ConfigureForFileLogging("Logfile.log", NLog.LogLevel.Off);
} 

Option 2 : LogManager.DisableLogging() and LogManager.EnableLogging() 选项2:LogManager.DisableLogging()和LogManager.EnableLogging()

from website Stopping-Starting-NLog-on-runtime 从网站Stopping-Starting-NLog-on-runtime

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

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