错误行未显示在stacktrace

时间:2018-06-23 07:57:42

标签: c# .net multithreading .net-core serilog

我在Serilog应用程序中使用.NetCore来编写软件在json文件上生成的异常,我创建了一个处理logger的类,这是我的实现:

using Serilog;
using Serilog.Exceptions;
using Serilog.Formatting.Json;
using System;
using System.IO;

namespace Utility
{
    public class Logger : Prop
    {
        /// <summary>
        /// Instance of the logger
        /// </summary>
        private ILogger logger;

        public Logger()
        {          
            logger = new LoggerConfiguration()
                .Enrich.WithExceptionDetails()
                .WriteTo.File(new JsonFormatter(renderMessage: true), "error.json")
                .CreateLogger();
        }

        /// <summary>
        /// Write the exception on a json file.
        /// </summary>
        /// <param name="ex">Exception generated.</param>
        public void Error(Exception ex)
        {
            //Write exception on a file.
            logger.Error("Exception: {ExceptionMessage}: " , ex, details);
            Environment.Exit(1);
        }
    }
}

该类非常简单,在构造函数中,我将创建logger的实例,该实例将生成文件error.json。有一种称为Error的方法,该方法将作为参数传递的异常写在error.json上,我采用格式输出here

我的应用程序具有以下结构:

public class Program 
{
    public static async Task Main(string[] args)
    {
         try
         { 
            Logger = new Logger();
            var result = await DoWorkAsync();

            if(result)
               Console.WriteLine("Jobs Done");
         }
         catch(Exception ex)
         {
            Logger.Error(ex);
         }
    }
}

方法DoWorkAsync()并行执行不同的任务,例如:

public async Task<bool> DoWorkAsync()
{
    Parallel.Invoke(
            async () => await Foo1(),
            async () => await Foo2(),
            () => Task.Run(async () => 
            { 
              await Foo3();
            }).ContinueWith(async (prev) => 
            {
               try
               {
                   await Foo4();
               }
               catch(Exception ex)
               {
                  Logger.Error(ex);
               }
            }));

    return true;
}

如您所见,前三个方法Foo1(), Foo2(), Foo3()是并行执行的,然后在Foo3()完成后,Foo4()方法开始,但是我想我需要另外一个try / catch块,这种实现对吗?

发生异常时,我得到以下输出:

  

[       {           “时间戳记”:“ 20-06-2018 16:17:57”,           “ Level”:“错误”,           “ MessageTemplate”:“异常:{ExceptionMessage}:           “ RenderedMessage”:“异常:\” System.NullReferenceException:对象引用未设置为对象的实例。\ n在App.Utility.MatchUtility.ParseGoalsEvent(HtmlNodeCollection容器,匹配项)\ n在Scraper.MatchScraper.d__1。 MoveNext()\ n ---从上一个引发异常的位置开始的堆栈末尾--- \ n

如您所见,它只给我生成了异常的方法的名称,而不是代码行,我做错了什么?

0 个答案:

没有答案