如何记录在 Azure 上部署的 WCF 服务中捕获的异常

发布于 2024-09-06 06:13:17 字数 35 浏览 0 评论 0原文

记录云托管的 WCF 服务中捕获的异常的最佳方法是什么?

What is the best way to log the exceptions caught in WCF service hosted on cloud?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

£烟消云散 2024-09-13 06:13:19

您可以利用 System.Diagnostics 并使用 Trace.traceError() 记录异常。然后,您可以安排这些跟踪语句定期上传到表存储(也许一分钟一次?),然后您可以使用本地应用程序或在辅助角色中运行的应用程序来检索和分析跟踪语句。

例如:在辅助角色的 OnStart() 中,自定义诊断管理器以将跟踪数据上传到表存储。在此示例中,它每分钟上传到 DiagnosticsConnectionString 中指定的存储帐户(默认情况下,设置为指向开发存储):

var diag = DiagnosticMonitor.GetDefaultInitialConfiguration();
diag.Logs.ScheduledTransferLogLevelFilter = LogLevel.Information;
diag.Logs.ScheduledTransferPeriod = TimeSpan.FromMinutes(1.0);
DiagnosticMonitor.Start("DiagnosticsConnectionString", diag);

然后,每当您在 WCF 服务中遇到异常时,将其记录下来:

System.Diagnostics.Trace.TraceError("WCF Error caught: ...");

最后,编写一些代码来查询诊断数据,或者使用新的内置 Visual Studio 存储资源管理器之类的工具来查看错误并采取措施。

You can take advantage of System.Diagnostics and logging your exceptions with Trace.traceError(). You can then schedule these trace statements to be periodically uploaded to table storage (maybe once a minute?), where you can then retrieve and analyze the trace statements either with an on-premise app or one running in a worker role.

For example: in your worker role's OnStart(), customize the Diagnostic Manager to upload your trace data to table storage. In this example, it's uploading every minute, to the storage account specified in DiagnosticsConnectionString (this is, by default, set up to point to dev storage):

var diag = DiagnosticMonitor.GetDefaultInitialConfiguration();
diag.Logs.ScheduledTransferLogLevelFilter = LogLevel.Information;
diag.Logs.ScheduledTransferPeriod = TimeSpan.FromMinutes(1.0);
DiagnosticMonitor.Start("DiagnosticsConnectionString", diag);

Then, whenever you encounter an exception in your WCF Service, log it:

System.Diagnostics.Trace.TraceError("WCF Error caught: ...");

Finally, either write some code to query the diagnostic data, or use something like the new built-in Visual Studio storage explorer to view and act on the errors.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文