Click here to Skip to main content
15,889,034 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
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.

C#
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);
          }
      }



Appreciated your help,
Nag
Posted
Updated 5-Nov-20 21:01pm
v3
Comments
Sergey Alexandrovich Kryukov 20-Aug-12 18:34pm    
Not enough information to analyze it on a very concrete level, so you can get some general advice.
--SA
[no name] 21-Aug-12 12:57pm    
It's probably more likely that your timer event is being pre-empted. And then running twice when it is able to run at all.

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
 
Share this answer
 
v5
If you think you might have two instances running, you can get the process id using the Process class in System.Diagnostics:

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


Add this to what you write to the log.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 21-Aug-12 1:12am    
Sometimes this technique is helpful, this information can be added to the logs; my 5.
--SA
Clifford Nelson 21-Aug-12 11:36am    
Definately not the whole answer, unlike yours. Thanks for the vote.
ureachnag 21-Aug-12 12:51pm    
@Sergey Alexandrovich Kryukov, Thank you very much for your reply, I did debug the code while attaching to the process, I have noticed that, _Ticker is calling twice. I have pasted the code, Appreciate if you help me ..
Thanks,
Nag
Sergey Alexandrovich Kryukov 21-Aug-12 14:39pm    
Nag,
You are very welcome, but if you want me to notice your message, please post it as a reply to my comment or solution, not Clifford's.
Also, I think you can accept the answer formally (green button). I would recommend you to accept both Clifford's and mine -- thanks.
--SA
Member 9129971 6-Jan-17 1:33am    
I have also this issue , and when i added process id then it shows the same process id in both.
I have solved this issue, I have noticed that after InitializeComponent();
I am calling the below lines of code.
string[] args = { };

this.OnStart(args);


Once I commented those lines it working fine.

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

Regards,
Nag
 
Share this answer
 
I had the same problem, however the cause of my duplicate logging (which took place inside an event) was that in my NLog.config file, I had two rules for a logger.

An extract of my NLog XML log file was as follows, and I made the mistake of adding a second rule (not shown below) rule that referred to the target name logfile - that is:

HTML
<logger name="*" minlevel="Info" writeTo="logfile" />


If you want it to log once and use an AsyncWrapper, your rule should only reference the Async Wrapper and not the nested target as well.

My fixed Nlog.config was something like this.

HTML
<targets>
  <target xsi:type="AsyncWrapper" name="asyncFile" overflowAction="Discard" queueLimit="10000" batchSize="200" timeToSleepBetweenBatches="1">
  <target   name="logfile"
            xsi:type="File"
            fileName="${var:logDirectory}/smarttracdesktop.log"
            archiveFileName="${var:logArchiveDirectory}/smarttracdesktop.{#}.log"
            archiveEvery="Day"
            archiveNumbering="Date"
            archiveDateFormat="yyyyMMdd"
            maxArchiveFiles="60"
            concurrentWrites="true"
            keepFileOpen="false"
            encoding="utf-8"
            layout="${var:defaultLayout}" />
  </target>
  <target name="logconsole" layout="${var:defaultLayout}" xsi:type="Console" />
</targets>
<rules>
  <logger name="*" minlevel="Info" writeTo="asyncFile" />
</rules>
 
Share this answer
 
Comments
Richard Deeming 6-Nov-20 8:26am    
The OP was clearly not using NLog - especially since it wasn't released until the year after this question was posted.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900