应用程序日志记录Application logging

检测代码不仅是获取用户见解的一种方式,也是了解应用程序中是否存在问题以及诊断需要修复的内容的唯一方法。Instrumenting your code is not only a way to gain insights about your users, but also the only way you can know whether something is wrong in your application, and to diagnose what needs to be fixed. 尽管在技术上可将调试器连接到生产服务,但这种做法并不常见。Although technically it's possible to connect a debugger to a production service, it's not a common practice. 因此,提供详细的检测数据非常重要。So, having detailed instrumentation data is important.

某些产品可自动检测代码。Some products automatically instrument your code. 尽管这些解决方案能够正常运行,但针对业务逻辑,几乎始终都要执行手动检测。Although these solutions can work well, manual instrumentation is almost always required to be specific to your business logic. 最后,必须提供足够的信息来对应用程序进行取证式的调试。In the end, you must have enough information to forensically debug the application. Service Fabric 应用程序可以使用任何记录框架进行检测。Service Fabric applications can be instrumented with any logging framework. 本文将介绍检测代码的几种不同方法,以及如何在不同的方法之间做出选择。This document describes a few different approaches to instrumenting your code, and when to choose one approach over another.

有关如何使用这些建议的示例,请参阅向 Service Fabric 应用程序添加日志记录For examples on how to use these suggestions, see Add logging to your Service Fabric application.

Application Insights SDKApplication Insights SDK

Application Insights 具有现成的与 Service Fabric 的丰富集成。Application Insights has a rich integration with Service Fabric out of the box. 用户可以添加 AI Service Fabric nuget 包并接收可以在 Azure 门户中查看的已创建和收集的数据和日志。Users can add the AI Service Fabric nuget packages and receive data and logs created and collected viewable in the Azure portal. 另外,建议用户添加其自己的遥测数据,以便诊断和调试其应用程序并跟踪哪些服务及其应用程序的哪些部分使用得最多。Additionally, users are encouraged to add their own telemetry in order to diagnose and debug their applications and track which services and parts of their application are used the most. 该 SDK 中的 TelemetryClient 类提供了许多方式来用于在应用程序中跟踪遥测数据。The TelemetryClient class in the SDK provides many ways to track telemetry in your applications. 请在我们提供的有关监视和诊断 .NET 应用程序的教程中查看有关如何进行检测以及向应用程序添加 Application Insights 的示例。Check out an example of how to instrument and add application insights to your application in our tutorial for monitoring and diagnosing a .NET application

EventSourceEventSource

在 Visual Studio 中通过模板创建 Service Fabric 解决方案时,将生成 EventSource 派生类(ServiceEventSourceActorEventSource)。When you create a Service Fabric solution from a template in Visual Studio, an EventSource-derived class (ServiceEventSource or ActorEventSource) is generated. 会创建一个模板,可将应用程序或服务的事件添加到其中。A template is created, in which you can add events for your application or service. EventSource 名称必须唯一,应该将它重命名,不要使用默认的模板字符串 MyCompany-<solution>-<project>。The EventSource name must be unique, and should be renamed from the default template string MyCompany-<solution>-<project>. 使用多个同名的 EventSource 定义会导致运行时出现问题。Having multiple EventSource definitions that use the same name causes an issue at run time. 每个定义的事件必须具有唯一标识符。Each defined event must have a unique identifier. 如果标识符不唯一,将发生运行时失败。If an identifier is not unique, a runtime failure occurs. 某些组织为标识符预先分配了值范围,避免不同的开发团队之间发生冲突。Some organizations preassign ranges of values for identifiers to avoid conflicts between separate development teams. 有关详细信息,请参阅 Vance 的博客MSDN 文档For more information, see Vance's blog or the MSDN documentation.

ASP.NET Core 日志记录ASP.NET Core logging

必须认真规划如何检测代码。It's important to carefully plan how you will instrument your code. 适当的检测计划有助于避免代码基变得不稳定,从而需要重新检测代码。The right instrumentation plan can help you avoid potentially destabilizing your code base, and then needing to reinstrument the code. 为了降低风险,可以选择一个检测库,例如,Microsoft ASP.NET Core 中包含的 Microsoft.Extensions.LoggingTo reduce risk, you can choose an instrumentation library like Microsoft.Extensions.Logging, which is part of Microsoft ASP.NET Core. ASP.NET Core 提供一个可在所选提供程序中使用的 ILogger 接口,同时尽量减轻对现有代码的影响。ASP.NET Core has an ILogger interface that you can use with the provider of your choice, while minimizing the effect on existing code. 可以在 Windows 和 Linux 上的 ASP.NET Core 中以及整个 .NET Framework 中使用代码,从而将检测代码标准化。You can use the code in ASP.NET Core on Windows and Linux, and in the full .NET Framework, so your instrumentation code is standardized.

后续步骤Next steps

选择了用于检测应用程序和服务的日志记录提供程序之后,需要聚合日志和事件才能将其发送到任何的分析平台。Once you have chosen your logging provider to instrument your applications and services, your logs and events need to be aggregated before they can be sent to any analysis platform. 阅读有关 Application InsightsEventFlow 的信息,以便更好地了解 Azure Monitor 推荐的一些选项。Read about Application Insights and EventFlow to better understand some of the Azure Monitor recommended options.