การแปลงข้อยกเว้นทั้งหมดเป็นสตริง
การโทรException.ToString()ให้ข้อมูลเพิ่มเติมกับคุณเพียงแค่ใช้Exception.Messageคุณสมบัติ อย่างไรก็ตามแม้จะยังมีข้อมูลจำนวนมากออกมาซึ่งรวมถึง:
- Dataคุณสมบัติคอลเลกชันที่พบในข้อยกเว้นทั้งหมด
- คุณสมบัติแบบกำหนดเองอื่น ๆ ที่เพิ่มลงในข้อยกเว้น
มีบางครั้งที่คุณต้องการเก็บข้อมูลพิเศษนี้ รหัสด้านล่างจัดการกับสถานการณ์ข้างต้น นอกจากนี้ยังเขียนคุณสมบัติของข้อยกเว้นในลำดับที่ดี ใช้ C # 7 แต่น่าจะง่ายสำหรับคุณที่จะแปลงเป็นเวอร์ชั่นเก่ากว่าถ้าจำเป็น ดูคำตอบที่เกี่ยวข้องนี้ด้วย
public static class ExceptionExtensions
{
    public static string ToDetailedString(this Exception exception) =>
        ToDetailedString(exception, ExceptionOptions.Default);
    public static string ToDetailedString(this Exception exception, ExceptionOptions options)
    {
        if (exception == null)
        {
            throw new ArgumentNullException(nameof(exception));
        } 
        var stringBuilder = new StringBuilder();
        AppendValue(stringBuilder, "Type", exception.GetType().FullName, options);
        foreach (PropertyInfo property in exception
            .GetType()
            .GetProperties()
            .OrderByDescending(x => string.Equals(x.Name, nameof(exception.Message), StringComparison.Ordinal))
            .ThenByDescending(x => string.Equals(x.Name, nameof(exception.Source), StringComparison.Ordinal))
            .ThenBy(x => string.Equals(x.Name, nameof(exception.InnerException), StringComparison.Ordinal))
            .ThenBy(x => string.Equals(x.Name, nameof(AggregateException.InnerExceptions), StringComparison.Ordinal)))
        {
            var value = property.GetValue(exception, null);
            if (value == null && options.OmitNullProperties)
            {
                if (options.OmitNullProperties)
                {
                    continue;
                }
                else
                {
                    value = string.Empty;
                }
            }
            AppendValue(stringBuilder, property.Name, value, options);
        }
        return stringBuilder.ToString().TrimEnd('\r', '\n');
    }
    private static void AppendCollection(
        StringBuilder stringBuilder,
        string propertyName,
        IEnumerable collection,
        ExceptionOptions options)
        {
            stringBuilder.AppendLine($"{options.Indent}{propertyName} =");
            var innerOptions = new ExceptionOptions(options, options.CurrentIndentLevel + 1);
            var i = 0;
            foreach (var item in collection)
            {
                var innerPropertyName = $"[{i}]";
                if (item is Exception)
                {
                    var innerException = (Exception)item;
                    AppendException(
                        stringBuilder,
                        innerPropertyName,
                        innerException,
                        innerOptions);
                }
                else
                {
                    AppendValue(
                        stringBuilder,
                        innerPropertyName,
                        item,
                        innerOptions);
                }
                ++i;
            }
        }
    private static void AppendException(
        StringBuilder stringBuilder,
        string propertyName,
        Exception exception,
        ExceptionOptions options)
    {
        var innerExceptionString = ToDetailedString(
            exception, 
            new ExceptionOptions(options, options.CurrentIndentLevel + 1));
        stringBuilder.AppendLine($"{options.Indent}{propertyName} =");
        stringBuilder.AppendLine(innerExceptionString);
    }
    private static string IndentString(string value, ExceptionOptions options)
    {
        return value.Replace(Environment.NewLine, Environment.NewLine + options.Indent);
    }
    private static void AppendValue(
        StringBuilder stringBuilder,
        string propertyName,
        object value,
        ExceptionOptions options)
    {
        if (value is DictionaryEntry)
        {
            DictionaryEntry dictionaryEntry = (DictionaryEntry)value;
            stringBuilder.AppendLine($"{options.Indent}{propertyName} = {dictionaryEntry.Key} : {dictionaryEntry.Value}");
        }
        else if (value is Exception)
        {
            var innerException = (Exception)value;
            AppendException(
                stringBuilder,
                propertyName,
                innerException,
                options);
        }
        else if (value is IEnumerable && !(value is string))
        {
            var collection = (IEnumerable)value;
            if (collection.GetEnumerator().MoveNext())
            {
                AppendCollection(
                    stringBuilder,
                    propertyName,
                    collection,
                    options);
            }
        }
        else
        {
            stringBuilder.AppendLine($"{options.Indent}{propertyName} = {value}");
        }
    }
}
public struct ExceptionOptions
{
    public static readonly ExceptionOptions Default = new ExceptionOptions()
    {
        CurrentIndentLevel = 0,
        IndentSpaces = 4,
        OmitNullProperties = true
    };
    internal ExceptionOptions(ExceptionOptions options, int currentIndent)
    {
        this.CurrentIndentLevel = currentIndent;
        this.IndentSpaces = options.IndentSpaces;
        this.OmitNullProperties = options.OmitNullProperties;
    }
    internal string Indent { get { return new string(' ', this.IndentSpaces * this.CurrentIndentLevel); } }
    internal int CurrentIndentLevel { get; set; }
    public int IndentSpaces { get; set; }
    public bool OmitNullProperties { get; set; }
}
เคล็ดลับยอดนิยม - ข้อยกเว้นการบันทึก
คนส่วนใหญ่จะใช้รหัสนี้เพื่อเข้าสู่ระบบ พิจารณาใช้SerilogกับSerilogของฉันข้อยกเว้น NuGet แพ็คเกจซึ่งบันทึกคุณสมบัติทั้งหมดของข้อยกเว้น แต่ทำได้เร็วกว่าและไม่มีภาพสะท้อนในกรณีส่วนใหญ่ Serilog เป็นกรอบการบันทึกขั้นสูงมากซึ่งเป็นความโกรธในเวลาที่เขียน
เคล็ดลับยอดนิยม - ร่องรอยสแต็กที่มนุษย์อ่านได้
คุณสามารถใช้Ben.Demystifierแพคเกจ NuGet ที่จะได้รับของมนุษย์ร่องรอยสแต็คที่สามารถอ่านได้สำหรับข้อยกเว้นหรือคุณserilog-enrichers-demystifyแพคเกจ NuGet ถ้าคุณกำลังใช้ Serilog