Skip to content

Commit

Permalink
Merge pull request #9 from dotnet-campus/t/lindexi/LogAbstractions
Browse files Browse the repository at this point in the history
加上抽象的日志
  • Loading branch information
walterlv authored Jul 11, 2021
2 parents f3470c5 + 09e9411 commit fa42a92
Show file tree
Hide file tree
Showing 7 changed files with 247 additions and 47 deletions.
49 changes: 41 additions & 8 deletions dotnetCampus.Logger.sln
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.26124.0
# Visual Studio Version 16
VisualStudioVersion = 16.0.30804.86
MinimumVisualStudioVersion = 15.0.26124.0
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{5D196596-756D-45C2-8A05-C8E4AB8A36E6}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "dotnetCampus.Logger", "src\dotnetCampus.Logger\dotnetCampus.Logger.csproj", "{F7ED61F4-920C-49EB-8DC1-74B2BE6AF272}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "dotnetCampus.Logger", "src\dotnetCampus.Logger\dotnetCampus.Logger.csproj", "{F7ED61F4-920C-49EB-8DC1-74B2BE6AF272}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{AFB0DF31-474C-4ACB-88C6-DD00552D5B5A}"
ProjectSection(SolutionItems) = preProject
build\Version.props = build\Version.props
README.md = README.md
CHANGELOG.md = CHANGELOG.md
Directory.Build.props = Directory.Build.props
README.md = README.md
build\Version.props = build\Version.props
EndProjectSection
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 All @@ -24,9 +28,6 @@ Global
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{F7ED61F4-920C-49EB-8DC1-74B2BE6AF272}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F7ED61F4-920C-49EB-8DC1-74B2BE6AF272}.Debug|Any CPU.Build.0 = Debug|Any CPU
Expand All @@ -40,8 +41,40 @@ Global
{F7ED61F4-920C-49EB-8DC1-74B2BE6AF272}.Release|x64.Build.0 = Release|Any CPU
{F7ED61F4-920C-49EB-8DC1-74B2BE6AF272}.Release|x86.ActiveCfg = Release|Any CPU
{F7ED61F4-920C-49EB-8DC1-74B2BE6AF272}.Release|x86.Build.0 = Release|Any CPU
{C9399790-B935-4CE3-A75B-4D99133E1DB1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C9399790-B935-4CE3-A75B-4D99133E1DB1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C9399790-B935-4CE3-A75B-4D99133E1DB1}.Debug|x64.ActiveCfg = Debug|Any CPU
{C9399790-B935-4CE3-A75B-4D99133E1DB1}.Debug|x64.Build.0 = Debug|Any CPU
{C9399790-B935-4CE3-A75B-4D99133E1DB1}.Debug|x86.ActiveCfg = Debug|Any CPU
{C9399790-B935-4CE3-A75B-4D99133E1DB1}.Debug|x86.Build.0 = Debug|Any CPU
{C9399790-B935-4CE3-A75B-4D99133E1DB1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C9399790-B935-4CE3-A75B-4D99133E1DB1}.Release|Any CPU.Build.0 = Release|Any CPU
{C9399790-B935-4CE3-A75B-4D99133E1DB1}.Release|x64.ActiveCfg = Release|Any CPU
{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}
EndGlobalSection
EndGlobal
35 changes: 0 additions & 35 deletions src/dotnetCampus.FileLogger/dotnetCampus.FileLogger/Program.cs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="dotnetCampus.AsyncWorkerCollection" Version="1.5.2" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="dotnetCampus.AsyncWorkerCollection" Version="1.6.2" />
</ItemGroup>
</Project>
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 fa42a92

Please sign in to comment.