如何设置Trace.Trace*
(Information
,Warning
,Error
等)以保存在Service Fabric日志中?我找到的唯一选择是使用ServiceEventSource
发送消息,但这将涉及修改大量代码以远离Trace语句。
我是否可以使用任何侦听器将Trace语句转发给ServiceEventSource?或者更简单的东西?
答案 0 :(得分:2)
EventFlow(如Peter Bons的评论中提到的)与Application Insights相结合可能是一个很好的解决方案。可以轻松设置EventFlow以收听现有的Trace语句,然后将其转发到Application Insighs,您可以在其中监视服务的执行情况。
设置EventFlow非常简单,只需将NuGet Microsoft.Diagnostics.EventFlow
添加到您的服务项目中即可。然后在eventflowconfig.json
中添加Trace作为输入,将Application Insights添加为输出:
{
"inputs": [
{
"type": "Trace",
"traceLevel": "Warning"
}
],
"filters": [],
"outputs": [
{
"type": "ApplicationInsights",
"instrumentationKey": "00000000-0000-0000-0000-000000000000"
}
],
"schemaVersion": "2016-08-11",
"extensions": []
}
现在,您只需在Azure帐户中设置Application Insights实例,并将instrumentationKey
更改为您的AI实例。
之后,您可以开始修改EventFlow配置以从Traces中提取特定的Request和Metric数据(如果您的跟踪中有这种类型的信息),那么您可以开始可视化它或在AI中搜索特定类型的跟踪仪表板。
请注意,Application Insights默认仅保留7天的日志。如果你想让你的痕迹更长,你可以改变AI层,你可以activate Continuous Export。
答案 1 :(得分:-2)
namespace MassProcessing
{
internal static class Program
{
/// <summary>
/// This is the entry point of the service host process.
/// </summary>
private static void Main()
{
try
{
// The ServiceManifest.XML file defines one or more service type names.
// Registering a service maps a service type name to a .NET type.
// When Service Fabric creates an instance of this service type,
// an instance of the class is created in this host process.
using (var diagnosticsPipeline = ServiceFabricDiagnosticPipelineFactory.CreatePipeline("EngageServiceFabric-MassProcessing-DiagnosticsPipeline"))
{
ServiceRuntime.RegisterServiceAsync("MassProcessingType",
context => new MassProcessing(context)).GetAwaiter().GetResult();
ServiceEventSource.Current.ServiceTypeRegistered(Process.GetCurrentProcess().Id, typeof(MassProcessing).Name);
// Prevents this host process from terminating so services keep running.
Thread.Sleep(Timeout.Infinite);
}
}
catch (Exception e)
{
ServiceEventSource.Current.ServiceHostInitializationFailed(e.ToString());
throw;
}
}
}
}