Windows服务事件视图日志写入两次. [英] Windows Services Event View Log writing Twice.

查看:90
本文介绍了Windows服务事件视图日志写入两次.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个Windows服务,该服务必须运行2分钟才能获取RSS提要并将其保存在数据库中.由于存在计时器,我没有任何调试服务的选项,当我检查事件视图日志和数据库时,该服务运行良好,但是事件视图日志消息出现问题,它正在写入两次,例如在8/20/2012 5:57:17 PM-更新了6条记录,我可以看到带有0条记录的相同时间戳.

任何机构都可以帮助解决此问题,我不知道该服务是运行两次还是编写事件日志两次.


--- ****我注意到ServiceTimer_Tick调用了两次,我附加了进程并进行了调试.
这是我的_Tick函数.

I have created a windows service which has to run for very 2 mins to get the RSS feeds and save it in the Database. I don''t have any option to debug the service because of the timer, when I check the Event View log and Data base the Service is working good, but the problem with event view log messages, it is writing twice, for example at 8/20/2012 5:57:17 PM - Updated with the 6 records and i can see that with the same time stamp with 0 records.

Any body please help to solve this issue, I don''t know whether the service is running twice or writing the event log twice.


---**** I have noticed that ServiceTimer_Tick is calling twice, I have attached the process and did the debug.
This is my _Tick function.

private void ServiceTimer_Tick(object sender, System.Timers.ElapsedEventArgs e)
      {
          try
          {
              lock (thisLock)
              {
                  if (TickerCounter > 1)
                  {
                      //tmrTimer.Enabled = true;
                      DateTime DBmaxPubdate = dbobj.GetMaxDate();
                      DateTime Currentpubdt = new DateTime();
                      RSSXMLLoad();
                      // this.eventLogObj.WriteEntry("RSSXMLLoad Completed");
                      Currentpubdt = GetMaxPubDateFromXML();
                      int dateresult = DateTime.Compare(MaxPubDate, DBmaxPubdate);
                      if (dateresult >= 0)
                      {
                          //this.eventLogObj.WriteEntry("RSS URL Service Timer Tick calling to SaveRSSToDB()");
                          //Thread processingThread = new Thread(SaveRSSToDB);
                          //processingThread.Start();

                          SaveRSSToDB();
                      }
                      //this.eventLogObj.WriteEntry("RSS URL Service: Scheduled Execution Completed.", EventLogEntryType.Information, 9, 1);
                      //tmrTimer.Enabled = true;
                  }
                  else
                  {
                      TickerCounter++;
                  }
              }
          }
          catch (Exception Ex)
          {
              this.eventLogObj.WriteEntry("RSS URL Service Timer Tick: Error Occured." + Environment.NewLine + Ex.Message + Environment.NewLine + Ex.StackTrace, EventLogEntryType.Error, 6, 2);
          }
      }




感谢您的帮助,
Nag




Appreciated your help,
Nag

推荐答案

这是记录日志的全部目的—揭示问题.如果它记录两次,则意味着调用此日志的代码上下文将被调用两次.如果不是这样,恭喜您:您已正确使用日志记录,因此它已帮助您发现缺陷.现在,将更多信息放在日志中,并找到解决方法.一种实用建议:在有问题的代码段中,可以通过检查这些点的调用堆栈来检查在应用程序运行时的不同阶段中调用的位置,以防万一没有异常要转储.为此,请使用类System.Diagnostics.StackTrace:
http://msdn.microsoft.com/zh-我们/library/system.diagnostics.stacktrace%28v=vs.100%29.aspx [ http://msdn.microsoft.com/en-us/library/system.diagnostics%28v = vs.100%29.aspx [
This is the whole purpose of logging — to reveal the problems. If it logs twice, it means the code context calling this log is called twice. If this is not supposed to be that way, my congratulations: you used logging properly, so it already helped you to detect a defect. Now, put more information in the logs and find out how to fix it. One practical advice: in the code fragment in question, you can examine where it is called in different stages of your application run time, by examining of the call stack in these points, in case when there are no exceptions to dump. To do that, please use the class System.Diagnostics.StackTrace:
http://msdn.microsoft.com/en-us/library/system.diagnostics.stacktrace%28v=vs.100%29.aspx[^].

Learn more from the facilities of System.Diagnostics; they are very useful:
http://msdn.microsoft.com/en-us/library/system.diagnostics%28v=vs.100%29.aspx[^].

In most cases, you should be able to use the debugger. The event log is used primarily in the cases when debugging is too difficult or the debugging session is not accessible for you, for example, if the process is executed only in your customer''s location which is out of your control. It''s the best to do as much debugging as you can using the Debugger on some development computer.

That''s all I could advise based on your limited information. You are quite welcome to ask some follow-up questions though.

Good luck,
—SA


如果您认为有两个实例正在运行,则可以使用System.Diagnostics中的Process 类获取进程ID:

If you think you might have two instances running, you can get the process id using the Process class in System.Diagnostics:

var p = Process.GetCurrentProcess();
var id = p.Id;





Add this to what you write to the log.


我已经解决了这个问题,我注意到在InitializeComponent();
之后 我正在调用以下代码行.
I have solved this issue, I have noticed that after InitializeComponent();
I am calling the below lines of code.
string[] args = { };

this.OnStart(args);



一旦我评论了这些行,就可以正常工作.

非常感谢Sergey Alexandrovich Kryukov和Clifford的帮助.

问候,
Nag



Once I commented those lines it working fine.

Thank you very much Sergey Alexandrovich Kryukov and Clifford for your help.

Regards,
Nag


这篇关于Windows服务事件视图日志写入两次.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆