2010-05-21 2 views
0

에 텍스트 파일을 만드는 방법을 내가윈도우 서비스

<config> 
<ServiceName>autorunquery</ServiceName> 
<DBConnection> 
    <server>servername</server> 
    <user>xyz</user> 
    <password>klM#2bs</password> 
<initialcatelog>TEST</initialcatelog> 

</DBConnection> 
<Log> 
    <logfilename>d:\testlogfile.txt</logfilename> 
</Log> 
<Frequency> 
    <value>10</value> 
    <unit>minute</unit> 
</Frequency> 
<CheckQuery>select * from credit_debit1 where station='Corporate'</CheckQuery> 
<Queries total="3"> 
    <Query id="1">Update credit_debit1 set station='xxx' where id=2</Query> 
<Query id="2">Update credit_debit1 set station='xxx' where id=4</Query> 
<Query id="3">Update credit_debit1 set station='xxx' where id=9</Query> 

</Queries> 
</config> 




using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Diagnostics; 
using System.Linq; 
using System.ServiceProcess; 
using System.Text; 
using System.IO; 
using System.Xml; 

namespace Service1 
{ 
    public partial class Service1 : ServiceBase 
    { 
     XmlTextReader reader = null; 
     string path = null; 
     FileStream fs = null; 
     StreamWriter sw = null; 
     public Service1() 
     { 
      InitializeComponent(); 
     } 

     protected override void OnStart(string[] args) 
     { 
      timer1.Enabled = true; 
      timer1.Interval = 10000; 
      timer1.Start(); 
      logfile("start service"); 


     } 

     protected override void OnStop() 
     { 
      timer1.Enabled = false; 
      timer1.Stop(); 
      logfile("stop service"); 

     } 
     private void logfile(string content) 
     { 

      try 
      { 
       reader = new XmlTextReader("queryconfig.xml");//xml file name which is in current directory 
       if (reader.ReadToFollowing("logfilename")) 
       { 
        path = reader.ReadElementContentAsString(); 
       } 
       fs = new FileStream(path, FileMode.Append, FileAccess.Write); 
       sw = new StreamWriter(fs); 
       sw.Write(content); 
       sw.WriteLine(DateTime.Now.ToString()); 

      } 
      catch (Exception ex) 
      { 
       sw.Write(ex.ToString()); 
       throw; 
      } 
      finally 
      { 
       if (reader != null) 
        reader.Close(); 
       if (sw != null) 
        sw.Close(); 
       if (fs != null) 
        fs.Close(); 
      } 
     } 

    } 
} 

내 문제는 파일이 생성되지 않는 것입니다 XML 파일이 있습니다.

+1

귀하의 질문에 대한 답변은 아니지만 몇 가지 일반적인 조언을드립니다. 'sw'가 생성되었다고 확신하지 않는 한 예외 처리기에서'sw.Write'를 호출하면 안됩니다. 일반적으로 로깅이 실패 할 때 오류를 기록하는 것은 좋지 않습니다. :) 로깅 메소드에서'reader'와'sw' 만 사용한다면, 멤버 변수 대신에 로컬 변수로 만들 수 있습니다. 그리고 그렇게하면 finally 문보다는'using'을 사용할 수 있습니다. –

답변

0

아마도 System.Windows.Forms.Timer를 사용하고 있기 때문에이 문제가 발생했다고 생각합니다. 그것은 Windows 서비스와 함께 작동하도록 설계되지 않았습니다. 타이머 구성 요소를 System.Timers.Timer로 변경하십시오. 이 클래스는 Windows 서비스에 적합합니다.

+0

아 맞습니다. Forms.Timer에는 창없는 Windows 서비스가 부족한 메시지 루프가 필요합니다. –

0

서비스 ID에는 HD에 쓸 수있는 권한이 없다고 가정합니다. 시스템 이벤트 로그에서 예외를 확인하십시오.

0

파일이 만들어지지 않으면 이벤트 로그에 나타날 수있는 예외가 발생할 가능성이 높으며 예외를 처리하지 않으면 서비스가 종료됩니다.

내 생각 엔 파일 이 (가)이지만 예상 한 위치에 생성되지 않았을 것입니다. 하드 코딩 된 절대 경로를 사용하거나 프로세스 탐색기를 사용하여 서비스의 작업 폴더를 확인하십시오.

디버그 정보를 내보내는 데 System.Diagnostics.Trace.WriteLine()을 사용하고 Sysinternals에서 Debug View을 시작하여 추적 메시지를 받고 표시하는 간단한 방법이 있습니다.