我在Windows 2012服务器上运行了一项服务。当我第一次启动该服务时,会生成一个日志条目并发送一封电子邮件。它仍在运行,但未生成任何通知。该服务假定观察SFTP文件夹并在csv文件传送时通知。第一个也是唯一的通知表明该文件已从tmp重命名为csv。为了使这项工作始终如一,我需要做些什么改变?
using System;
using System.Configuration;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Net.Mail;
using System.Net.Mime;
using System.ServiceProcess;
namespace FileWatcherService
{
public partial class fileWatcherService : ServiceBase
{
private FileSystemWatcher m_Watcher;
private bool m_bDirty;
public fileWatcherService()
{
InitializeComponent();
if (!EventLog.SourceExists("DFC Windows File Service"))
{
EventLog.CreateEventSource("DFC Windows File Service", "DFC Log");
}
eventLogger.Source = "DFC Windows File Service";
eventLogger.Log = "DFC Log";
var sftp_input = ConfigurationManager.AppSettings["WatcherPath"];
m_Watcher = new FileSystemWatcher(sftp_input, "LoanTape*.csv");
m_Watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
| NotifyFilters.FileName | NotifyFilters.DirectoryName;
m_Watcher.Changed += OnChanged;
m_Watcher.Created += OnChanged;
m_Watcher.Deleted += OnChanged;
m_Watcher.Renamed += OnRenamed;
m_Watcher.EnableRaisingEvents = true;
}
protected override void OnStart(string[] args)
{
try
{
eventLogger.WriteEntry("DFC Loan Tape file watcher service started");
}
catch (Exception e)
{
eventLogger.WriteEntry("Loan Tape file watcher: Error in startup: " + e.Message);
}
}
protected override void OnStop()
{
eventLogger.WriteEntry("DFC Loan Tape file watcher service stopped");
}
private void OnChanged(object sender, FileSystemEventArgs e)
{
if (!m_bDirty)
{
string emailMsg = "Loan Tape: " + e.Name + " is available for processing";
var email_distribution = ConfigurationManager.AppSettings["EmailRecipients"].Split(',');
var serverName = ConfigurationManager.AppSettings["ServerName"];
SendMessage(serverName + " DFC: Loan Tape Available", emailMsg, email_distribution);
m_bDirty = true;
eventLogger.WriteEntry("DFC Loan Tape available");
}
}
private void OnRenamed(object sender, RenamedEventArgs e)
{
if (!m_bDirty)
{
string emailMsg = "Loan Tape: " + e.Name + " is available for processing";
var email_distribution = ConfigurationManager.AppSettings["EmailRecipients"].Split(',');
var serverName = ConfigurationManager.AppSettings["ServerName"];
SendMessage(serverName + " DFC: Loan Tape Available", emailMsg, email_distribution);
m_bDirty = true;
eventLogger.WriteEntry("DFC Loan Tape available");
m_Watcher.Filter = e.Name;
m_Watcher.Path = e.FullPath.Substring(0, e.FullPath.Length - m_Watcher.Filter.Length);
}
}
private void SendMessage(string msgSubject, string msgBody, string[] email_distribution = null)
{
if (email_distribution == null)
{
email_distribution = ConfigurationManager.AppSettings["EmailRecipients"].Split(',');
}
MailMessage mailMsg = new MailMessage();
foreach (string email in email_distribution)
{
mailMsg.To.Add(new MailAddress(email));
}
mailMsg.From = new MailAddress(ConfigurationManager.AppSettings["EmailSender"], "DFC Loan Tape Service");
mailMsg.Subject = msgSubject;
mailMsg.IsBodyHtml = true;
mailMsg.Body = msgSubject + ":" + Environment.NewLine + msgBody;
mailMsg.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(msgBody, new ContentType("text/html")));
try
{
SmtpClient smtpClient = new SmtpClient("smtp.sendgrid.net", Convert.ToInt32(587));
NetworkCredential credentials = new NetworkCredential(username, password);
smtpClient.Credentials = credentials;
smtpClient.Send(mailMsg);
}
catch (Exception e)
{
eventLogger.WriteEntry("Error in sending Email msg" + Environment.NewLine + "Error is: " + e.Message);
}
}
}
}
答案 0 :(得分:1)
而不是寻找错误,修复你的逻辑。
如果设置了标志,您只在处理程序中工作。问题是您的处理程序设置了此标志,这意味着它们在第一次之后永远不会工作。
if (!m_bDirty)
{
// some work
m_bDirty = true;
// some other work
}
答案 1 :(得分:0)
很难从你提供的内容中理解整个逻辑,但是
(deftemplate flag (slot country) (multislot colors))
(deffacts countries
(flag (country USA) (colors red white blue))
(flag (country Belgium) (colors black yellow red))
(flag (country Poland) (colors white red))
(flag (country Monaco) (colors white red))
(flag (country Sweden) (colors yellow blued))
(flag (country Panama) (colors red white blue))
(flag (country Jamaica) (colors black yellow green))
(flag (country Columbia) (colors yellow blue red))
(flag (country Italy) (colors green white red))
(flag (country Greece) (colors blue white))
(flag (country Botswana) (colors blue white black)))
(deffacts start
(get-color))
(defrule get-colors
?f <- (get-color)
=>
(retract ?f)
(printout t "Flag color (done to end)?")
(assert (new-color (read))))
(defrule find-matches
(find-flag)
(exists (color ?))
(flag (country ?country))
(forall (color ?color)
(flag (country ?country) (colors $? ?color $?)))
=>
(printout t ?country " 's flag contains the specified colors." crlf))
(defrule no-matches
(find-flag)
(not (and (flag (country ?country))
(forall (color ?color)
(flag (country ?country) (colors $? ?color $?)))))
=>
(printout t "No country's flag contains the specified colors." crlf))
(defrule all-flags-match
(find-flag)
(not (color ?))
=>
(printout t "No search colors were specified." crlf))
(defrule look-for-flags
?f <- (new-color done)
=>
(retract ?f)
(assert (find-flag)))
(defrule look-for-another-color
?f <- (new-color ?color&~done)
=>
(assert (color ?color))
(retract ?f)
(assert (get-color)))
更改
OnRenamed
因此你需要在检查其是否为假之前在其他方法的开头将其更改为false,但由于缺少信息,很难给出一个好的答案
答案 2 :(得分:0)
认为你遇到的问题是事件会不断发生,因为你已经迷上了filewatcher的所有事件。你需要收集事件并一个接一个地解雇它们。 1.在构造函数中启动一个线程:
Thread thread = new Thread(new ThreadStart(this.DoWork));
thread.Start();
创建一个eventdata类来保存消息和事件数据。
私有类FileEventData { public string FileName {get;组; } public string Message {get;组; } }
创建一个并发列表来保存数据。
private ConcurrentBag fileEventDatas = new ConcurrentBag();
线程现在将开始从列表中发送事件:
private void DoWork()
{
//control for shutdown using a flag
while (true)
{
if (fileEventDatas.Count > 0)
{
FileEventData data;
fileEventDatas.TryTake(out data);
SendMessage(serverName + " DFC: Loan Tape Available", data.Message, this.email_distribution);
eventLogger.WriteEntry("DFC Loan Tape available");
}
}
}
&#13;
private void OnChanged(object sender, FileSystemEventArgs e)
{
string emailMsg = "Loan Tape: " + e.Name + " is available for processing";
FileEventData fileEventData = new FileEventData()
{
FileName = e.Name,
Message = emailMsg,
};
this.fileEventDatas.Add(fileEventData);
}
&#13;