2009-07-09 51 views
0

好吧,所以我想爲運行在ASP.net和C#上的網站提供錯誤日誌文件。如何在服務器上存儲文件ASP.net

我試過用常規路徑存儲驅動器。 期待它,因爲它是在C#代碼(頁面後面的代碼)它會拿起服務器並將其存儲在那裏。 (最終它是在服務器上執行的代碼)

不幸的是,對我來說那不是真的。它被保存在客戶端打開網站的本地機器上。

如何強制它被保存在服務器機器上?

我想在一個地方的所有錯誤日誌。 關於閱讀或如何做到這一點的任何建議。

string path = "C:\\WebSite"; 
    string error = "error";   
    try 
    { 
     // check if dir exists if it does i add file name to the path 
     // i removed code to keep it simple 
     path += "\\logs.txt"; 

     FileStream log_fs = 
       new FileStream(path, FileMode.Append, FileAccess.Write); 
     StreamWriter log_sw = 
       new StreamWriter(log_fs); 
     string log = String.Empty; 

     // formate log string using error message 

     log_sw.Write(log); 
     log_sw.Close(); 
     log_fs.Close(); 
    } 
    catch (Exception ex) 
    { 
     // here I e-mail the error in case logging failed 
    } 

此代碼將生成的,而不是服務器的本地計算機上的文件,

+0

代碼示例請嗎? – cjk 2009-07-09 15:27:47

回答

0

您可以登錄到Web服務器上的任何位置(該ASPNET用戶帳戶具有寫權限)的代碼,例如這樣:

string log = String.Empty; 

// format log string using error message 

try 
{ 
    using (StreamWriter logFile = new StreamWriter(@"C:\WebSite\logs.txt", true)) 
    { 
     logFile.WriteLine(log); 
    } 
} 
catch 
{ 
    // here I e-mail the error in case logging failed 
} 

這會將文件寫入托管網站的Web服務器。現在,如果您在開發期間在本地運行網站,並且您正在使用內置的VS Web服務器或IIS,那麼當您在本地測試網站時,您的計算機上將創建該文件。

0

要回答你的問題的建議部分,你有沒有考慮使用ELMAH,它將錯誤記錄到不同的存儲:

的Microsoft SQL Server 甲骨文(OracleErrorLog) SQLite的(第3版)數據庫文件 的Microsoft Access (AccessErrorLog) VistaDB的(VistaDBErrorLog) 寬鬆的XML文件 RAM(內存)

或通過電子郵件發送,微博

elmah非常容易設置和非常有效

相關問題