[英]How do I update my web.config for sending error through email in C#?
我正在使用Visual Studio 2013,Web服務器正在運行.NET 4,並且在實時服務器上的Web窗體上出現錯誤,該代碼可以在localhost上正常運行。
當我通過本地主機調試我的Web應用程序時,當前是通過電子郵件發送錯誤,我希望它在運行時(如果可能,還可以在我的本地主機上)向我發送錯誤。 它曾經從生產服務器向我發送錯誤,但是當我開始調試時,它更改了一些設置,但現在不再。
我假設問題出在某個地方,任何人都可以就在哪里尋找問題給出一些一般性想法嗎? 這是我的web.config中的一些用於異常處理的代碼,還有其他一些代碼可以幫助您解決問題嗎?
<loggingConfiguration name="Logging Application Block" tracingEnabled="true" defaultCategory="General" logWarningsWhenNoCategoriesMatch="true">
<listeners>
<add toAddress="cynthiat@myurl.com" fromAddress="sysadmin@myurl.com" subjectLineStarter="" subjectLineEnder="" smtpServer="###.###.###.###" smtpPort="##" formatter="Text Formatter" listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.EmailTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=f890890fsfds" traceOutputOptions="None" filter="All" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.EmailTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=f890890fsfds" name="Email TraceListener" />
<add source="Enterprise Library Logging" formatter="Text Formatter" log="Application" machineName="" listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FormattedEventLogTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=f890890fsfds5" traceOutputOptions="None" filter="All" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FormattedEventLogTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=f890890fsfds" name="Formatted EventLog TraceListener" />
</listeners>
<formatters>
<add template="Extended Properties: {dictionary({key} - {value}
)}
Message: {message}



Timestamp: {timestamp}

Severity: {severity}

Machine: {machine}


Process Id: {processId}

Process Name: {processName}

Win32 Thread Id: {win32ThreadId}

Thread Name: {threadName}

" type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" name="Text Formatter" />
</formatters>
<categorySources>
<add switchValue="All" name="General">
<listeners>
<add name="Formatted EventLog TraceListener" />
</listeners>
</add>
</categorySources>
<specialSources>
<allEvents switchValue="All" name="All Events">
<listeners>
<add name="Email TraceListener" />
</listeners>
</allEvents>
<notProcessed switchValue="All" name="Unprocessed Category" />
<errors switchValue="All" name="Logging Errors & Warnings">
<listeners>
<add name="Formatted EventLog TraceListener" />
</listeners>
</errors>
</specialSources>
</loggingConfiguration>
<exceptionHandling>
<exceptionPolicies>
<add name="Global Policy">
<exceptionTypes>
<add type="System.Exception, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=f890890fsfds" postHandlingAction="NotifyRethrow" name="Exception">
<exceptionHandlers>
<add logCategory="General" eventId="100" severity="Error" title="Enterprise Library Exception Handling" formatterType="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.TextExceptionFormatter, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=4.1.0.0, Culture=neutral, PublicKeyToken=f890890fsfds" priority="0" useDefaultLogger="false" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging.LoggingExceptionHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=f890890fsfds" name="Logging Handler" />
</exceptionHandlers>
</add>
</exceptionTypes>
</add>
</exceptionPolicies>
如果沒有看到更多的配置文件和/或發送電子郵件通知的錯誤處理代碼,我們將無法對您的特定問題提供太多幫助。 但是,解決您問題的另一種方法可能是錯誤處理/記錄/通知框架,例如:
ELMAH:ASP.NET的錯誤記錄模塊和處理程序(也包括MVC!)
NuGet包- https://www.nuget.org/packages/elmah/
來源-https: //code.google.com/p/elmah/
雲日志-https: //elmah.io
Microsoft 企業庫
記錄應用程序塊
異常處理應用程序塊
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.