Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

控制台中的日志也打印完整的异常信息 #18

Merged
merged 5 commits into from
Aug 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/dotnetCampus.Logger/Writers/ConsoleLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ private void LogCore(LogLevel logLevel, Exception? exception, string message, Fu
};
ConsoleMultilineMessage($"""
{message}
{tag}{exception.GetType().Name}: {exception.Message}
{tag}{exception}
""", formatter);
}
}
Expand Down Expand Up @@ -140,14 +140,14 @@ private void ClearAndMoveToLastLine(int repeatCount)
}

private const string Reset = C.Reset;
private const string TraceText = F.Magenta;
private const string DebugText = F.White;
private const string TraceText = F.BrightBlack;
private const string InformationText = F.Green + D.Bold;
private const string WarningText = F.Yellow;
private const string ErrorText = F.BrightRed;
private const string CriticalText = F.Red;

private static string TraceTag => $"{B.Black}{F.BrightBlack}[{DateTime.Now:HH:mm:ss.fff}]{Reset}";
private static string TraceTag => $"{B.Magenta}{F.White}[{DateTime.Now:HH:mm:ss.fff}]{Reset}";
private static string DebugTag => $"{B.BrightBlack}{F.White}[{DateTime.Now:HH:mm:ss.fff}]{Reset}";
private static string InformationTag => $"{B.Green}{F.Black}[{DateTime.Now:HH:mm:ss.fff}]{Reset}";
private static string WarningTag => $"{B.Yellow}{F.Black}[{DateTime.Now:HH:mm:ss.fff}]{Reset}";
Expand Down
67 changes: 36 additions & 31 deletions src/dotnetCampus.Logger/Writers/Helpers/TagFilterManager.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;

namespace dotnetCampus.Logging.Writers.Helpers;
Expand Down Expand Up @@ -109,54 +110,58 @@ internal bool IsTagEnabled(string text)
/// <param name="args">命令行参数。</param>
public static TagFilterManager? FromCommandLineArgs(string[] args)
{
if (!TryGetCommandLineValue(args, LogTagParameterName, out var value))
{
return null;
}

HashSet<string> anyFilterTags = [];
HashSet<string> includingFilterTags = [];
HashSet<string> excludingFilterTags = [];
for (var i = 0; i < args.Length; i++)
var filterTags = value.Split([',', ';', ' ']);
foreach (var tag in filterTags)
{
if (args[i] != LogTagParameterName || i + 1 >= args.Length)
{
continue;
}

var filterTags = args[i + 1].Split([',', ';', ' ']);
foreach (var tag in filterTags)
if (tag.StartsWith("-", StringComparison.Ordinal))
{
if (tag.StartsWith("-", StringComparison.Ordinal))
{
#if NET8_0_OR_GREATER
excludingFilterTags.Add(tag[1..]);
excludingFilterTags.Add(tag[1..]);
#else
excludingFilterTags.Add(tag.Substring(1));
excludingFilterTags.Add(tag.Substring(1));
#endif
}
else if (tag.StartsWith("+", StringComparison.Ordinal))
{
}
else if (tag.StartsWith("+", StringComparison.Ordinal))
{
#if NET8_0_OR_GREATER
includingFilterTags.Add(tag[1..]);
includingFilterTags.Add(tag[1..]);
#else
includingFilterTags.Add(tag.Substring(1));
includingFilterTags.Add(tag.Substring(1));
#endif
}
else
{
anyFilterTags.Add(tag);
}
}

return new TagFilterManager
else
{
AnyFilterTags = anyFilterTags.ToImmutableHashSet(),
IncludingFilterTags = includingFilterTags.ToImmutableHashSet(),
ExcludingFilterTags = excludingFilterTags.ToImmutableHashSet(),
};
anyFilterTags.Add(tag);
}
}

return new TagFilterManager
{
AnyFilterTags = [],
IncludingFilterTags = [],
ExcludingFilterTags = [],
AnyFilterTags = anyFilterTags.ToImmutableHashSet(),
IncludingFilterTags = includingFilterTags.ToImmutableHashSet(),
ExcludingFilterTags = excludingFilterTags.ToImmutableHashSet(),
};
}

private static bool TryGetCommandLineValue(string[] args, string parameterName, [NotNullWhen(true)] out string? value)
{
for (var i = 0; i < args.Length; i++)
{
if (string.Equals(args[i], parameterName, StringComparison.OrdinalIgnoreCase) && i + 1 < args.Length)
{
value = args[i + 1];
return true;
}
}
value = null;
return false;
}
}