From 09e94113542ee2f3162124396f363927c6b4311e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9E=97=E5=BE=B7=E7=86=99?= Date: Sun, 4 Jul 2021 12:06:52 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8A=A0=E4=B8=8A=E6=8A=BD=E8=B1=A1=E7=9A=84?= =?UTF-8?q?=E6=97=A5=E5=BF=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 解决 dotnet runtime 没有 .NET Framework 4.5 的支持,同时也解决如果新建一个日志库会添加多一个dll文件 --- dotnetCampus.Logger.sln | 15 +++ .../EventId.cs | 103 ++++++++++++++++++ .../ILogger.cs | 28 +++++ .../LogLevel.cs | 58 ++++++++++ .../dotnetCampus.Logger.Abstractions.csproj | 14 +++ 5 files changed, 218 insertions(+) create mode 100644 src/dotnetCampus.Logger.Abstractions/EventId.cs create mode 100644 src/dotnetCampus.Logger.Abstractions/ILogger.cs create mode 100644 src/dotnetCampus.Logger.Abstractions/LogLevel.cs create mode 100644 src/dotnetCampus.Logger.Abstractions/dotnetCampus.Logger.Abstractions.csproj diff --git a/dotnetCampus.Logger.sln b/dotnetCampus.Logger.sln index a5ea44f..1628268 100644 --- a/dotnetCampus.Logger.sln +++ b/dotnetCampus.Logger.sln @@ -17,6 +17,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "dotnetCampus.FileLogger", "src\dotnetCampus.FileLogger\dotnetCampus.FileLogger\dotnetCampus.FileLogger.csproj", "{C9399790-B935-4CE3-A75B-4D99133E1DB1}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "dotnetCampus.Logger.Abstractions", "src\dotnetCampus.Logger.Abstractions\dotnetCampus.Logger.Abstractions.csproj", "{73E5A5FB-39A6-4417-9E66-1B9F6E925CFE}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -51,6 +53,18 @@ Global {C9399790-B935-4CE3-A75B-4D99133E1DB1}.Release|x64.Build.0 = Release|Any CPU {C9399790-B935-4CE3-A75B-4D99133E1DB1}.Release|x86.ActiveCfg = Release|Any CPU {C9399790-B935-4CE3-A75B-4D99133E1DB1}.Release|x86.Build.0 = Release|Any CPU + {73E5A5FB-39A6-4417-9E66-1B9F6E925CFE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {73E5A5FB-39A6-4417-9E66-1B9F6E925CFE}.Debug|Any CPU.Build.0 = Debug|Any CPU + {73E5A5FB-39A6-4417-9E66-1B9F6E925CFE}.Debug|x64.ActiveCfg = Debug|Any CPU + {73E5A5FB-39A6-4417-9E66-1B9F6E925CFE}.Debug|x64.Build.0 = Debug|Any CPU + {73E5A5FB-39A6-4417-9E66-1B9F6E925CFE}.Debug|x86.ActiveCfg = Debug|Any CPU + {73E5A5FB-39A6-4417-9E66-1B9F6E925CFE}.Debug|x86.Build.0 = Debug|Any CPU + {73E5A5FB-39A6-4417-9E66-1B9F6E925CFE}.Release|Any CPU.ActiveCfg = Release|Any CPU + {73E5A5FB-39A6-4417-9E66-1B9F6E925CFE}.Release|Any CPU.Build.0 = Release|Any CPU + {73E5A5FB-39A6-4417-9E66-1B9F6E925CFE}.Release|x64.ActiveCfg = Release|Any CPU + {73E5A5FB-39A6-4417-9E66-1B9F6E925CFE}.Release|x64.Build.0 = Release|Any CPU + {73E5A5FB-39A6-4417-9E66-1B9F6E925CFE}.Release|x86.ActiveCfg = Release|Any CPU + {73E5A5FB-39A6-4417-9E66-1B9F6E925CFE}.Release|x86.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -58,6 +72,7 @@ Global GlobalSection(NestedProjects) = preSolution {F7ED61F4-920C-49EB-8DC1-74B2BE6AF272} = {5D196596-756D-45C2-8A05-C8E4AB8A36E6} {C9399790-B935-4CE3-A75B-4D99133E1DB1} = {5D196596-756D-45C2-8A05-C8E4AB8A36E6} + {73E5A5FB-39A6-4417-9E66-1B9F6E925CFE} = {5D196596-756D-45C2-8A05-C8E4AB8A36E6} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {D7D96521-5EB7-45B4-B70D-2B3FB69A3082} diff --git a/src/dotnetCampus.Logger.Abstractions/EventId.cs b/src/dotnetCampus.Logger.Abstractions/EventId.cs new file mode 100644 index 0000000..aecafef --- /dev/null +++ b/src/dotnetCampus.Logger.Abstractions/EventId.cs @@ -0,0 +1,103 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace dotnetCampus.Logger.Abstractions +{ + /// + /// Identifies a logging event. The primary identifier is the "Id" property, with the "Name" property providing a short description of this type of event. + /// + /// Copy from dotnet runtime +#if LogPublicAsInternal + internal +#else + public +#endif + readonly struct EventId + { + /// + /// Implicitly creates an EventId from the given . + /// + /// The to convert to an EventId. + public static implicit operator EventId(int i) + { + return new EventId(i); + } + + /// + /// Checks if two specified instances have the same value. They are equal if they have the same Id. + /// + /// The first . + /// The second . + /// if the objects are equal. + public static bool operator ==(EventId left, EventId right) + { + return left.Equals(right); + } + + /// + /// Checks if two specified instances have different values. + /// + /// The first . + /// The second . + /// if the objects are not equal. + public static bool operator !=(EventId left, EventId right) + { + return !left.Equals(right); + } + + /// + /// Initializes an instance of the struct. + /// + /// The numeric identifier for this event. + /// The name of this event. + public EventId(int id, string? name = null) + { + Id = id; + Name = name; + } + + /// + /// Gets the numeric identifier for this event. + /// + public int Id { get; } + + /// + /// Gets the name of this event. + /// + public string? Name { get; } + + /// + public override string ToString() + { + return Name ?? Id.ToString(); + } + + /// + /// Indicates whether the current object is equal to another object of the same type. Two events are equal if they have the same id. + /// + /// An object to compare with this object. + /// if the current object is equal to the other parameter; otherwise, . + public bool Equals(EventId other) + { + return Id == other.Id; + } + + /// + public override bool Equals(object? obj) + { + if (obj is null) + { + return false; + } + + return obj is EventId eventId && Equals(eventId); + } + + /// + public override int GetHashCode() + { + return Id; + } + } +} diff --git a/src/dotnetCampus.Logger.Abstractions/ILogger.cs b/src/dotnetCampus.Logger.Abstractions/ILogger.cs new file mode 100644 index 0000000..173963f --- /dev/null +++ b/src/dotnetCampus.Logger.Abstractions/ILogger.cs @@ -0,0 +1,28 @@ +using System; + +namespace dotnetCampus.Logger.Abstractions +{ + /// + /// Represents a type used to perform logging. + /// + /// Aggregates most logging patterns to a single method. + /// Copy from dotnet runtime +#if LogPublicAsInternal + internal +#else + public +#endif + interface ILogger + { + /// + /// Writes a log entry. + /// + /// Entry will be written on this level. + /// Id of the event. + /// The entry to be written. Can be also an object. + /// The exception related to this entry. + /// Function to create a message of the and . + /// The type of the object to be written. + void Log(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func formatter); + } +} diff --git a/src/dotnetCampus.Logger.Abstractions/LogLevel.cs b/src/dotnetCampus.Logger.Abstractions/LogLevel.cs new file mode 100644 index 0000000..59ab39e --- /dev/null +++ b/src/dotnetCampus.Logger.Abstractions/LogLevel.cs @@ -0,0 +1,58 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace dotnetCampus.Logger.Abstractions +{ + /// + /// Defines logging severity levels. + /// + /// Copy from dotnet runtime +#if LogPublicAsInternal + internal +#else + public +#endif + enum LogLevel + { + /// + /// Logs that contain the most detailed messages. These messages may contain sensitive application data. + /// These messages are disabled by default and should never be enabled in a production environment. + /// + Trace = 0, + + /// + /// Logs that are used for interactive investigation during development. These logs should primarily contain + /// information useful for debugging and have no long-term value. + /// + Debug = 1, + + /// + /// Logs that track the general flow of the application. These logs should have long-term value. + /// + Information = 2, + + /// + /// Logs that highlight an abnormal or unexpected event in the application flow, but do not otherwise cause the + /// application execution to stop. + /// + Warning = 3, + + /// + /// Logs that highlight when the current flow of execution is stopped due to a failure. These should indicate a + /// failure in the current activity, not an application-wide failure. + /// + Error = 4, + + /// + /// Logs that describe an unrecoverable application or system crash, or a catastrophic failure that requires + /// immediate attention. + /// + Critical = 5, + + /// + /// Not used for writing log messages. Specifies that a logging category should not write any messages. + /// + None = 6, + } +} diff --git a/src/dotnetCampus.Logger.Abstractions/dotnetCampus.Logger.Abstractions.csproj b/src/dotnetCampus.Logger.Abstractions/dotnetCampus.Logger.Abstractions.csproj new file mode 100644 index 0000000..9bc4a26 --- /dev/null +++ b/src/dotnetCampus.Logger.Abstractions/dotnetCampus.Logger.Abstractions.csproj @@ -0,0 +1,14 @@ + + + + netcoreapp3.1;net45;net5.0 + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + +