others - C# - 程序停止生成日志文件,但是仍在运行(间歇性)

Quickbooks不允许Windows服务通过其QBFC13 0连接到其系统,我尝试过一切方法,我有一个由用户手动启动的Windows应用程序(exe ),它执行的工作确实成功了(有时)。

我无法理解它如何停止写入日志(或真正执行操作),但是它仍在任务管理器中运行,此外当它停止时不会写入日志,也不会捕获错误。

我没有做代码清理,所以如果代码没有组织好,我很抱歉。





static void Main(string[] args)


 {


 WriteToFile("Program Startup" + DateTime.Now);



 try


 {



 var DebugLevel = System.Configuration.ConfigurationManager.AppSettings["Debug"];


 while (1 > 0)


 {


 Thread.Sleep(1000 * 60);


 WriteToFile("Timer Ticked" + DateTime.Now);


 if ((DateTime.Now.Hour == 4 && DateTime.Now.Minute == 0 && attempt == -1) || DebugLevel =="DEBUG" || (attempt <= 2 && attempt >= 0))


 {


 GC.Collect();


 GC.WaitForPendingFinalizers();


 try


 {


 attempt++;//iterate the attempt


 WriteToFile("QuickBooks data gather executable started" + DateTime.Now +" Attempt Number:" + Convert.ToString(attempt + 1));



 SetupQBSession();


 QBCustomer customerQuery = new QBCustomer();


 List<ICustomerRet> customers = customerQuery.DoCustomerQuery(sessionManager, connectionOpen, sessionBegun);


 WriteToFile("Initial Connection to Quickbooks succesfull." + DateTime.Now);


 foreach (ICustomerRet r in customers)


 {


 //WriteToFile(r.Name.GetValue());


 //Make database Call to log QB customer data.


 using (CSTClassesDataContext cst = new CSTClassesDataContext(ConfigurationManager.ConnectionStrings["CST"].ConnectionString))


 {


 //save the customer data


 var DBCustomerSaveResult = cst.QB_Customer_Save(r.Name.GetValue(), (bool)r.IsActive.GetValue());


 WriteToFile("QuickBooks Customer Data Saved for" + r.Name.GetValue() +"" + DateTime.Now);



 string connetionString = null;


 SqlConnection connection;


 SqlCommand command;


 string sql = null;


 SqlDataReader dataReader;


 connetionString = cst.Connection.ConnectionString;


 sql ="select * from QB_customer where QB_customer.customer_name = '" + r.Name.GetValue() +"'";


 connection = new SqlConnection(connetionString);


 try


 {


 connection.Open();


 command = new SqlCommand(sql, connection);


 dataReader = command.ExecuteReader();



 if (dataReader.Read() && dataReader.HasRows)


 {


 QBInvoice invoicesQuery = new QBInvoice();


 List<IInvoiceRet> invoices = invoicesQuery.DoInvoiceQuery(dataReader.GetValue(1).ToString(), sessionManager, connectionOpen, sessionBegun);


 foreach (IInvoiceRet invoice in invoices)


 {



 if (invoice.Memo.GetValue().ToString() != null)


 {


 int dbQBcustomerID = Convert.ToInt32(dataReader.GetValue(0));


 var DBInvoiceSaveResult = cst.QB_invoice_save(dbQBcustomerID, invoice.RefNumber.GetValue().ToString(), invoice.Memo.GetValue().ToString(), (decimal)invoice.BalanceRemaining.GetValue(), invoice.DueDate.GetValue(), invoice.IsPaid.GetValue());


 WriteToFile("Invoices updated for QB_customer_id:" + dbQBcustomerID +", Invoice Number" + invoice.RefNumber.GetValue().ToString() +", IsPaid" + invoice.IsPaid.GetValue() +"" + DateTime.Now);


 }


 }


 //Console.WriteLine(dataReader.GetValue(0) +" -" + dataReader.GetValue(1) +" -" + dataReader.GetValue(2));



 }


 dataReader.Close();


 command.Dispose();


 connection.Close();


 }


 catch (Exception ex)


 {


 WriteToFile(ex.ToString());



 }



 }


 }


 CloseQBSession();


 attempt = -1;


 }


 catch (Exception exeption)


 {


 if (attempt <= 2 && attempt >= 0)


 {


 //attempt++;


 }


 else


 {


 attempt = -1;


 }


 CloseQBSession();


 WriteToFile(exeption.ToString());


 }


 finally


 {


 WriteToFile("QB Thread Finished" + DateTime.Now);


 }


 }


 }


 }


 catch(Exception high)


 {


 WriteToFile(high.ToString());


 }


 // Hide


 ShowWindow(handle, SW_HIDE);


 //Console.Read();



 }


 private static void WriteToFile(string Message)


 {



 string filepath ="FILEPATH REMOVED FOR STACK OVERFLOW(unneeded information)" + DateTime.Now.Date.ToShortDateString().Replace('/', '_') +".txt";


 if (!File.Exists(filepath))


 {


 // Create a file to write to. 


 using (StreamWriter sw = File.CreateText(filepath))


 {


 sw.WriteLine(Message);


 }


 }


 else


 {


 using (StreamWriter sw = File.AppendText(filepath))


 {


 sw.WriteLine(Message);


 }


 }


 }



时间: 作者:

使用事件日志,导航到Event Viewer > Window Logs > Application,然后应用程序会在最后一次运行时查找你的应用程序,你可以看到那里发生了什么。

作者:
...