Skip to content

Commit

Permalink
添加serilog支持
Browse files Browse the repository at this point in the history
  • Loading branch information
brucehu123 committed Jan 12, 2020
1 parent 5afb872 commit 8236ad9
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/Surging.Core/Surging.Core.Serilog/SerilogModule.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using Microsoft.Extensions.Logging;
using Serilog;
using Serilog.Formatting.Elasticsearch;
using Surging.Core.CPlatform;
using Surging.Core.CPlatform.Module;
using Surging.Core.ServiceHosting.Internal;

namespace Surging.Core.Serilog
{
public class SerilogModule: EnginePartModule
{
public override void Initialize(AppModuleContext context)
{
var serviceProvider = context.ServiceProvoider;
base.Initialize(context);
var section = AppConfig.GetSection("Serilog");

var logger = new LoggerConfiguration().ReadFrom.Configuration(section)
//.WriteTo.RollingFile(new ElasticsearchJsonFormatter(renderMessageTemplate:false),"c:/logs/log-{Date}.log")
//.WriteTo.Logger(config => {
// config.Filter.ByIncludingOnly(evt=>evt.Level== Serilog.Events.LogEventLevel.Information).WriteTo.RollingFile()
//})
.CreateLogger();

serviceProvider.GetInstances<ILoggerFactory>().AddSerilog(logger);
serviceProvider.GetInstances<IApplicationLifetime>().ApplicationStopped.Register(Log.CloseAndFlush);
}

//public override void Dispose()
//{
// //Log.CloseAndFlush();
// base.Dispose();
//}
}
}
35 changes: 35 additions & 0 deletions src/Surging.Core/Surging.Core.Serilog/SerilogProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//using Microsoft.Extensions.Logging;
//using System;
//using System.Collections.Concurrent;
//using System.Collections.Generic;
//using System.Text;
//using Serilog;
//using Serilog.Core;
//using Microsoft.Extensions.Configuration;

//namespace Surging.Core.Serilog
//{
// public class SerilogProvider : ILoggerProvider
// {
// private readonly ConcurrentDictionary<string, Logger> _loggers = new ConcurrentDictionary<string, Logger>();
// private readonly IConfiguration _config;

// public SerilogProvider(IConfiguration configuration)
// {
// _config = configuration;
// }

// public Microsoft.Extensions.Logging.ILogger CreateLogger(string categoryName)
// {
// var log = new LoggerConfiguration()
// .ReadFrom.Configuration(_config)
// .CreateLogger();
// throw new NotImplementedException();
// }

// public void Dispose()
// {
// _loggers.Clear();
// }
// }
//}
23 changes: 23 additions & 0 deletions src/Surging.Core/Surging.Core.Serilog/Serilogger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//using Microsoft.Extensions.Logging;
//using System;

//namespace Surging.Core.Serilog
//{
// public class Serilogger : Microsoft.Extensions.Logging.ILogger
// {
// public IDisposable BeginScope<TState>(TState state)
// {
// throw new NotImplementedException();
// }

// public bool IsEnabled(LogLevel logLevel)
// {
// throw new NotImplementedException();
// }

// public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)
// {
// throw new NotImplementedException();
// }
// }
//}
19 changes: 19 additions & 0 deletions src/Surging.Core/Surging.Core.Serilog/Surging.Core.Serilog.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Serilog" Version="2.9.0" />
<PackageReference Include="Serilog.Extensions.Logging" Version="3.0.1" />
<PackageReference Include="Serilog.Formatting.Elasticsearch" Version="8.0.1" />
<PackageReference Include="Serilog.Settings.Configuration" Version="3.1.0" />
<PackageReference Include="Serilog.Sinks.RollingFile" Version="3.3.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Surging.Core.CPlatform\Surging.Core.CPlatform.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
<ProjectReference Include="..\..\Surging.Core\Surging.Core.Protocol.Udp\Surging.Core.Protocol.Udp.csproj" />
<ProjectReference Include="..\..\Surging.Core\Surging.Core.Protocol.WS\Surging.Core.Protocol.WS.csproj" />
<ProjectReference Include="..\..\Surging.Core\Surging.Core.ProxyGenerator\Surging.Core.ProxyGenerator.csproj" />
<ProjectReference Include="..\..\Surging.Core\Surging.Core.Serilog\Surging.Core.Serilog.csproj" />
<ProjectReference Include="..\..\Surging.Core\Surging.Core.ServiceHosting.Extensions\Surging.Core.ServiceHosting.Extensions.csproj" />
<ProjectReference Include="..\..\Surging.Core\Surging.Core.ServiceHosting\Surging.Core.ServiceHosting.csproj" />
<ProjectReference Include="..\..\Surging.Core\Surging.Core.Swagger\Surging.Core.Swagger.csproj" />
Expand Down Expand Up @@ -96,4 +97,6 @@
<Folder Include="Properties\" />
</ItemGroup>

<ProjectExtensions><VisualStudio><UserProperties surgingsettings_1json__JsonSchema="..ElasticsearchJsonFormatter" /></VisualStudio></ProjectExtensions>

</Project>
7 changes: 7 additions & 0 deletions src/Surging.sln
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Surging.Core.ServiceHosting
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Surging.Core.Grpc", "Surging.Core\Surging.Core.Grpc\Surging.Core.Grpc.csproj", "{609AEF75-8FB2-4BB6-8A6A-8107151F871E}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Surging.Core.Serilog", "Surging.Core\Surging.Core.Serilog\Surging.Core.Serilog.csproj", "{6F45517A-4465-4F68-89B5-BB315EAD7CCD}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -281,6 +283,10 @@ Global
{609AEF75-8FB2-4BB6-8A6A-8107151F871E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{609AEF75-8FB2-4BB6-8A6A-8107151F871E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{609AEF75-8FB2-4BB6-8A6A-8107151F871E}.Release|Any CPU.Build.0 = Release|Any CPU
{6F45517A-4465-4F68-89B5-BB315EAD7CCD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6F45517A-4465-4F68-89B5-BB315EAD7CCD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6F45517A-4465-4F68-89B5-BB315EAD7CCD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6F45517A-4465-4F68-89B5-BB315EAD7CCD}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -327,6 +333,7 @@ Global
{E603580C-FF82-4705-BE71-B48303971D6E} = {E3ADE8DE-5F3A-4E76-B22F-2C9435AACA06}
{3FDA38C5-2F88-422B-A32F-DE1E0F002B35} = {E3ADE8DE-5F3A-4E76-B22F-2C9435AACA06}
{609AEF75-8FB2-4BB6-8A6A-8107151F871E} = {E3ADE8DE-5F3A-4E76-B22F-2C9435AACA06}
{6F45517A-4465-4F68-89B5-BB315EAD7CCD} = {E3ADE8DE-5F3A-4E76-B22F-2C9435AACA06}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {9889BCDC-E6A6-448F-B01A-ECFD0FAF294C}
Expand Down

0 comments on commit 8236ad9

Please sign in to comment.