Skip to content

Commit

Permalink
加上抽象的日志
Browse files Browse the repository at this point in the history
解决 dotnet runtime 没有 .NET Framework 4.5 的支持,同时也解决如果新建一个日志库会添加多一个dll文件
  • Loading branch information
lindexi committed Jul 4, 2021
1 parent 3673665 commit 09e9411
Show file tree
Hide file tree
Showing 5 changed files with 218 additions and 0 deletions.
15 changes: 15 additions & 0 deletions dotnetCampus.Logger.sln
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -51,13 +53,26 @@ 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
EndGlobalSection
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}
Expand Down
103 changes: 103 additions & 0 deletions src/dotnetCampus.Logger.Abstractions/EventId.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace dotnetCampus.Logger.Abstractions
{
/// <summary>
/// Identifies a logging event. The primary identifier is the "Id" property, with the "Name" property providing a short description of this type of event.
/// </summary>
/// Copy from dotnet runtime
#if LogPublicAsInternal
internal
#else
public
#endif
readonly struct EventId
{
/// <summary>
/// Implicitly creates an EventId from the given <see cref="int"/>.
/// </summary>
/// <param name="i">The <see cref="int"/> to convert to an EventId.</param>
public static implicit operator EventId(int i)
{
return new EventId(i);
}

/// <summary>
/// Checks if two specified <see cref="EventId"/> instances have the same value. They are equal if they have the same Id.
/// </summary>
/// <param name="left">The first <see cref="EventId"/>.</param>
/// <param name="right">The second <see cref="EventId"/>.</param>
/// <returns><see langword="true" /> if the objects are equal.</returns>
public static bool operator ==(EventId left, EventId right)
{
return left.Equals(right);
}

/// <summary>
/// Checks if two specified <see cref="EventId"/> instances have different values.
/// </summary>
/// <param name="left">The first <see cref="EventId"/>.</param>
/// <param name="right">The second <see cref="EventId"/>.</param>
/// <returns><see langword="true" /> if the objects are not equal.</returns>
public static bool operator !=(EventId left, EventId right)
{
return !left.Equals(right);
}

/// <summary>
/// Initializes an instance of the <see cref="EventId"/> struct.
/// </summary>
/// <param name="id">The numeric identifier for this event.</param>
/// <param name="name">The name of this event.</param>
public EventId(int id, string? name = null)
{
Id = id;
Name = name;
}

/// <summary>
/// Gets the numeric identifier for this event.
/// </summary>
public int Id { get; }

/// <summary>
/// Gets the name of this event.
/// </summary>
public string? Name { get; }

/// <inheritdoc />
public override string ToString()
{
return Name ?? Id.ToString();
}

/// <summary>
/// Indicates whether the current object is equal to another object of the same type. Two events are equal if they have the same id.
/// </summary>
/// <param name="other">An object to compare with this object.</param>
/// <returns><see langword="true" /> if the current object is equal to the other parameter; otherwise, <see langword="false" />.</returns>
public bool Equals(EventId other)
{
return Id == other.Id;
}

/// <inheritdoc />
public override bool Equals(object? obj)
{
if (obj is null)
{
return false;
}

return obj is EventId eventId && Equals(eventId);
}

/// <inheritdoc />
public override int GetHashCode()
{
return Id;
}
}
}
28 changes: 28 additions & 0 deletions src/dotnetCampus.Logger.Abstractions/ILogger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System;

namespace dotnetCampus.Logger.Abstractions
{
/// <summary>
/// Represents a type used to perform logging.
/// </summary>
/// <remarks>Aggregates most logging patterns to a single method.</remarks>
/// Copy from dotnet runtime
#if LogPublicAsInternal
internal
#else
public
#endif
interface ILogger
{
/// <summary>
/// Writes a log entry.
/// </summary>
/// <param name="logLevel">Entry will be written on this level.</param>
/// <param name="eventId">Id of the event.</param>
/// <param name="state">The entry to be written. Can be also an object.</param>
/// <param name="exception">The exception related to this entry.</param>
/// <param name="formatter">Function to create a <see cref="string"/> message of the <paramref name="state"/> and <paramref name="exception"/>.</param>
/// <typeparam name="TState">The type of the object to be written.</typeparam>
void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func<TState, Exception?, string> formatter);
}
}
58 changes: 58 additions & 0 deletions src/dotnetCampus.Logger.Abstractions/LogLevel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace dotnetCampus.Logger.Abstractions
{
/// <summary>
/// Defines logging severity levels.
/// </summary>
/// Copy from dotnet runtime
#if LogPublicAsInternal
internal
#else
public
#endif
enum LogLevel
{
/// <summary>
/// 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.
/// </summary>
Trace = 0,

/// <summary>
/// Logs that are used for interactive investigation during development. These logs should primarily contain
/// information useful for debugging and have no long-term value.
/// </summary>
Debug = 1,

/// <summary>
/// Logs that track the general flow of the application. These logs should have long-term value.
/// </summary>
Information = 2,

/// <summary>
/// Logs that highlight an abnormal or unexpected event in the application flow, but do not otherwise cause the
/// application execution to stop.
/// </summary>
Warning = 3,

/// <summary>
/// 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.
/// </summary>
Error = 4,

/// <summary>
/// Logs that describe an unrecoverable application or system crash, or a catastrophic failure that requires
/// immediate attention.
/// </summary>
Critical = 5,

/// <summary>
/// Not used for writing log messages. Specifies that a logging category should not write any messages.
/// </summary>
None = 6,
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netcoreapp3.1;net45;net5.0</TargetFrameworks>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="dotnetCampus.SourceYard" Version="0.1.19369-alpha">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

</Project>

0 comments on commit 09e9411

Please sign in to comment.