forked from zsh2401/AutumnBox
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
327 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
* ============================================================================== | ||
* | ||
* Filename: Aogger | ||
* Description: | ||
* | ||
* Version: 1.0 | ||
* Created: 2020/8/18 10:38:22 | ||
* Compiler: Visual Studio 2019 | ||
* | ||
* Author: zsh2401 | ||
* | ||
* ============================================================================== | ||
*/ | ||
using AutumnBox.Logging.Management; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
|
||
namespace AutumnBox.Logging | ||
{ | ||
/// <summary> | ||
/// 自动化获取调用者信息 | ||
/// </summary> | ||
public static class Aogger | ||
{ | ||
public static void WriteToLog(this Exception e, object? additionMessage = null) | ||
{ | ||
SLogger.Warn(CallerQuerier.Get(2).TypeName, additionMessage ?? String.Empty, e); | ||
} | ||
public static void Info(object message) | ||
{ | ||
SLogger.Info(CallerQuerier.Get(2).TypeName, message); | ||
} | ||
public static void Warn(object message) | ||
{ | ||
SLogger.Warn(CallerQuerier.Get(2).TypeName, message); | ||
} | ||
public static void Exception(Exception e) | ||
{ | ||
SLogger.Exception(CallerQuerier.Get(2).TypeName, e); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
/* | ||
* ============================================================================== | ||
* | ||
* Filename: CallerQuerier | ||
* Description: | ||
* | ||
* Version: 1.0 | ||
* Created: 2020/8/18 10:08:46 | ||
* Compiler: Visual Studio 2019 | ||
* | ||
* Author: zsh2401 | ||
* | ||
* ============================================================================== | ||
*/ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics; | ||
using System.Text; | ||
|
||
namespace AutumnBox.Logging | ||
{ | ||
public static class CallerQuerier | ||
{ | ||
public static Result Default { get; } = new Result("Unknow", String.Empty); | ||
public static Result GetCurrent() | ||
{ | ||
return Get(2); | ||
} | ||
public static Result Get(int startAt) | ||
{ | ||
Result? result = null; | ||
var frames = new StackTrace().GetFrames(); | ||
for (int i = startAt; i < frames.Length; i++) | ||
{ | ||
var current = At(i); | ||
if (current.TypeName.StartsWith("<>c")) | ||
continue; | ||
else | ||
{ | ||
result = current; | ||
break; | ||
} | ||
} | ||
return result ?? Default; | ||
Result At(int index) | ||
{ | ||
return new Result( | ||
frames[index]?.GetMethod()?.DeclaringType?.Name ?? String.Empty, | ||
frames[index]?.GetMethod()?.Name ?? String.Empty | ||
); | ||
} | ||
} | ||
public sealed class Result : IEquatable<Result?> | ||
{ | ||
public Result(string typeName, string methodName) | ||
{ | ||
TypeName = typeName ?? throw new ArgumentNullException(nameof(typeName)); | ||
MethodName = methodName ?? throw new ArgumentNullException(nameof(methodName)); | ||
} | ||
|
||
public string TypeName { get; } | ||
public string MethodName { get; } | ||
|
||
public override bool Equals(object? obj) | ||
{ | ||
return Equals(obj as Result); | ||
} | ||
|
||
public bool Equals(Result? other) | ||
{ | ||
return other != null && | ||
TypeName == other.TypeName && | ||
MethodName == other.MethodName; | ||
} | ||
|
||
public override int GetHashCode() | ||
{ | ||
return HashCode.Combine(TypeName, MethodName); | ||
} | ||
|
||
public static bool operator ==(Result? left, Result? right) | ||
{ | ||
return EqualityComparer<Result>.Default.Equals(left, right); | ||
} | ||
|
||
public static bool operator !=(Result? left, Result? right) | ||
{ | ||
return !(left == right); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
* ============================================================================== | ||
* | ||
* Filename: AoggerTest | ||
* Description: | ||
* | ||
* Version: 1.0 | ||
* Created: 2020/8/18 10:43:57 | ||
* Compiler: Visual Studio 2019 | ||
* | ||
* Author: zsh2401 | ||
* | ||
* ============================================================================== | ||
*/ | ||
using AutumnBox.Logging; | ||
using AutumnBox.Logging.Management; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics; | ||
using System.Linq; | ||
using System.Text; | ||
|
||
namespace AutumnBox.Tests.Logging | ||
{ | ||
[TestClass] | ||
public class AoggerTest : ICoreLogger | ||
{ | ||
public void Dispose() | ||
{ | ||
LoggingManager.Use(_defaultCoreLogger); | ||
} | ||
|
||
private ICoreLogger _defaultCoreLogger; | ||
|
||
public AoggerTest() | ||
{ | ||
_defaultCoreLogger = LoggingManager.CoreLogger; | ||
LoggingManager.Use(this); | ||
} | ||
|
||
[TestMethod] | ||
public void Info() | ||
{ | ||
Aogger.Info("f"); | ||
var last = logs.Last(); | ||
Assert.AreEqual(nameof(AoggerTest), last.Category); | ||
Assert.AreEqual(nameof(Aogger.Info), last.Level); | ||
} | ||
|
||
private readonly List<ILog> logs = new List<ILog>(); | ||
public void Log(ILog log) | ||
{ | ||
logs.Add(log); | ||
Debug.WriteLine(log.ToFormatedString()); | ||
} | ||
} | ||
} |
Oops, something went wrong.