ServiceEventSource和Service Fabric

时间:2017-11-26 17:37:21

标签: azure-service-fabric

使用非常简单的代码来处理异常处理和登录Service Fabric:

public async Task<IActionResult> Get()
{
    ServiceEventSource.Current.ServiceMessage(serviceContext, $"Entered 'GetAll' method");

    IActionResult result = null;
    List<Customer> list = new List<Customer>();

    try
    {
        using (CustomersContext context = new CustomersContext(connectionString))
        {
            list = context.Customers.Where(a => a.IsDeleted == false).ToList<Customer>();
        }
        result = Json(list);

        throw new ArgumentException("Argument exception", new NullReferenceException("Null reference exception"));
    }
    catch (Exception ex)
    {
        result = StatusCode(StatusCodes.Status500InternalServerError);
        ServiceEventSource.Current.ServiceMessage(serviceContext, $"Exception: " + JsonConvert.SerializeObject(ex));
    }
    finally
    {
        ServiceEventSource.Current.ServiceMessage(serviceContext, $"Exiting 'GetAll' method");
    }
    return result;
}

我的问题是:为什么这行代码在开头

ServiceEventSource.Current.ServiceMessage(serviceContext, $"Entered 'GetAll' method");

和finally块中的这行代码

ServiceEventSource.Current.ServiceMessage(serviceContext, $"Exiting 'GetAll' method");

在诊断事件中正常工作并记录条目,而在catch块中这是

ServiceEventSource.Current.ServiceMessage(serviceContext, $"Exception: " + JsonConvert.SerializeObject(ex));

出现以下错误:&#34; ServiceEventSource错误CS0103:名称&#39; ServiceEventSource&#39;在当前上下文中不存在&#34;?

谢谢!

1 个答案:

答案 0 :(得分:0)

解决

分析被调用方法的签名和代码,我们注意到一件事:

public void ServiceMessage(ServiceContext serviceContext, string message, params objects[] args)

这意味着message参数用于string.Format方法。

我的问题是传递带有“{”和“}”字符的Json字符串会引发异常,因为它无法识别格式。