.NetCore手动封装日志组件

背景

1、支持写入文本和写入ELK;

2、支持.netcore3.1和 .Net5(也可支持.Net6)

3、基于Serilog 开源的日志组件进行开发;将预定义的配置,逻辑代码都写在组件里,项目开发者无需关注其逻辑。

4、封装的目的是便于在项目里更加简单方便使用;组件专注于组件本身逻辑,项目开发专注于项目逻辑代码---》分工明确,扩展性又好,组件可以给多个项目共同使用;

 

代码实现

1、VS2019创建类库项目 MuXue.Extensions.Serilog,双击项目,修改一些只,比如 TargetFramework变成TargetFrameworks,写入netcoreapp3.1;net5.0;配置 ItemGroup Condition,分别分配 .netcore3.1和.net5的包。

代码贴出来:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFrameworks>netcoreapp3.1;net5.0</TargetFrameworks>
    <Company>MuXue.Duozubao</Company>
    <Authors>lipu</Authors>
    <Version>1.0.6</Version>
    <Description>沐雪多租宝-日志打入到elk系统里。可以兼容3.1和5.0</Description>
    <PackageIcon>icon.png</PackageIcon>
    <PackageIconUrl />
    <GeneratePackageOnBuild>true</GeneratePackageOnBuild>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Serilog.AspNetCore" Version="4.1.0" />
    <PackageReference Include="Serilog.Extensions.Logging" Version="3.1.0" />
    <PackageReference Include="Serilog.Extensions.Logging.File" Version="2.0.0" />
    <PackageReference Include="Serilog.Sinks.Elasticsearch" Version="8.4.1" />
  </ItemGroup>
  <ItemGroup Condition="'$(TargetFramework)' == 'netcoreapp3.1'">
    <FrameworkReference Include="Microsoft.AspNetCore.App" />
    <PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="3.1.8" />
    <PackageReference Include="Microsoft.Extensions.Logging" Version="3.1.8" />
  </ItemGroup>

  <ItemGroup Condition="'$(TargetFramework)' == 'net5.0'">
    <FrameworkReference Include="Microsoft.AspNetCore.App" />
    <PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="5.0.0" />
    <PackageReference Include="Microsoft.Extensions.Logging" Version="5.0.0" />
  </ItemGroup>
  <ItemGroup>
    <None Include="..\icon.png">
      <Pack>True</Pack>
      <PackagePath></PackagePath>
    </None>
  </ItemGroup>

</Project> 

 2、Serilog日志既支持.输入文本,也支持 ELK

 

posted @ 2022-03-04 14:48  沐雪架构师  阅读(207)  评论(0编辑  收藏  举报