使用這個例子對我的MVC專案,我想注冊一個檔案我的例外,甚至在不附加一個除錯器,當我將其發布在IIS上,而不是在輸出視窗時,該專案處于除錯運行后,我的專案。
我的代碼與鏈接中顯示的代碼相同。
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Web;
namespace ServerSide
{
public class MyLoggerService : DevExpress.XtraReports.Web.ClientControls.LoggerService
{
public override void Info(string message)
{
System.Diagnostics.Debug.WriteLine("[{0}]: Info: '{1}'.", DateTime.Now, message);
}
public override void Error(Exception exception, string message)
{
System.Diagnostics.Debug.WriteLine("[{0}]: Exception occured. Message: '{1}'. Exception Details:\r\n{2}",
DateTime.Now, message, exception);
}
}
}
我如何更改此代碼以使其將例外記錄到檔案中?
uj5u.com熱心網友回復:
如果有人需要它,解決方案是在 Error 方法中添加以下代碼:
string filePath = @"C:\ReportDesigner\server\publish\Exceptions.txt";
using (StreamWriter writer = new StreamWriter(filePath, true))
{
writer.WriteLine("-----------------------------------------------------------------------------");
writer.WriteLine("Date : " DateTime.Now.ToString());
writer.WriteLine();
while (exception != null)
{
writer.WriteLine(exception.GetType().FullName);
writer.WriteLine("Message : " exception.Message);
writer.WriteLine("StackTrace : " exception.StackTrace);
exception = exception.InnerException;
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/381017.html
上一篇:如何從C 上的分段錯誤中恢復?
