-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from Portkey-Wallet/hotfix/im-apollo-integration
apollo integration
- Loading branch information
Showing
7 changed files
with
198 additions
and
24 deletions.
There are no files selected for viewing
32 changes: 32 additions & 0 deletions
32
src/MessagePush.Application/Common/ConfigurationProvidersHelper.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
using System; | ||
using System.Linq; | ||
using Microsoft.Extensions.Configuration; | ||
using Serilog; | ||
using Volo.Abp; | ||
|
||
namespace MessagePush.Common; | ||
|
||
public static class ConfigurationProvidersHelper | ||
{ | ||
/// <summary> | ||
/// The Method displays the enabled configuration providers in the order they were added, | ||
/// configuration providers that are added later have higher priority and override previous key settings. | ||
/// </summary> | ||
/// <param name="context"></param> | ||
public static void DisplayConfigurationProviders(ApplicationInitializationContext context) | ||
{ | ||
try | ||
{ | ||
var configuration = context.GetConfiguration(); | ||
var configurationRoot = (IConfigurationRoot)configuration; | ||
foreach (var provider in configurationRoot.Providers.ToList()) | ||
{ | ||
Log.Warning("ConfigurationProvider: {0}", provider.ToString()); | ||
} | ||
} | ||
catch (Exception e) | ||
{ | ||
Log.Error(e, "display configuration providers error."); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
using Serilog; | ||
using Serilog.Events; | ||
|
||
namespace MessagePush.Common; | ||
|
||
public static class LogHelper | ||
{ | ||
public static ILogger CreateLogger(LogEventLevel logEventLevel) | ||
{ | ||
return new LoggerConfiguration() | ||
.MinimumLevel.Is(logEventLevel) | ||
.Enrich.FromLogContext() | ||
.WriteTo.Async(c => | ||
c.Console( | ||
outputTemplate: | ||
"[{Timestamp:yyyy-MM-dd HH:mm:ss.fff}{Offset:zzz}][{Level:u3}] [{SourceContext}] {Message}{NewLine}{Exception}")) | ||
.WriteTo.Async(c => c.File("Logs/log-.log", rollingInterval: RollingInterval.Day, | ||
outputTemplate: | ||
"[{Timestamp:yyyy-MM-dd HH:mm:ss.fff}{Offset:zzz}][{Level:u3}] [{SourceContext}] {Message}{NewLine}{Exception}")) | ||
.CreateLogger(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
117 changes: 117 additions & 0 deletions
117
src/MessagePush.Application/MessagePushHostBuilderExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
using Com.Ctrip.Framework.Apollo.Logging; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.Hosting; | ||
using Serilog; | ||
using Serilog.Events; | ||
|
||
namespace MessagePush; | ||
|
||
public static class MessagePushHostBuilderExtensions | ||
{ | ||
public static IHostBuilder UseApolloForConfigureHostBuilder(this IHostBuilder hostBuilder) | ||
{ | ||
return hostBuilder | ||
.ConfigureAppConfiguration((_, builder) => { builder.AddJsonFile("appsettings.apollo.json"); }) | ||
.ConfigureAppConfiguration((context, builder) => | ||
{ | ||
if (!context.Configuration.GetValue<bool>("IsApolloEnabled", false)) | ||
{ | ||
return; | ||
} | ||
|
||
//To display the Apollo console logs | ||
#if DEBUG | ||
LogManager.UseConsoleLogging(Com.Ctrip.Framework.Apollo.Logging.LogLevel.Trace); | ||
#endif | ||
|
||
builder.AddApollo(builder.Build().GetSection("apollo")); | ||
}) | ||
.ConfigureGloballySharedLog(false); | ||
} | ||
|
||
private static IHostBuilder ConfigureGloballySharedLog(this IHostBuilder hostBuilder, bool configureService) | ||
{ | ||
return configureService | ||
? hostBuilder.ConfigureServices((context, _) => | ||
{ | ||
ConfigureGloballySharedLog(context.Configuration); | ||
SetGloballySharedLoggerForApolloLogs(); | ||
}) | ||
: hostBuilder.ConfigureAppConfiguration((context, _) => | ||
{ | ||
ConfigureGloballySharedLog(context.Configuration); | ||
SetGloballySharedLoggerForApolloLogs(); | ||
}); | ||
} | ||
|
||
private static void ConfigureGloballySharedLog(IConfiguration configuration) | ||
{ | ||
Log.Logger = new LoggerConfiguration() | ||
#if DEBUG | ||
.MinimumLevel.Debug() | ||
#else | ||
.MinimumLevel.Information() | ||
#endif | ||
.MinimumLevel.Override("Microsoft", LogEventLevel.Information) | ||
.MinimumLevel.Override("Microsoft.EntityFrameworkCore", LogEventLevel.Warning) | ||
.Enrich.FromLogContext() | ||
.ReadFrom.Configuration(configuration) | ||
#if DEBUG | ||
.WriteTo.Async(c => c.Console()) | ||
#endif | ||
.CreateLogger(); | ||
} | ||
|
||
private static void SetGloballySharedLoggerForApolloLogs() | ||
{ | ||
LogManager.LogFactory = name => (level, message, exception) => | ||
{ | ||
var messageTemplate = $"{name} {message}"; | ||
switch (level) | ||
{ | ||
case LogLevel.Information: | ||
Log.Information(messageTemplate); | ||
break; | ||
case LogLevel.Warning: | ||
Log.Warning(messageTemplate); | ||
break; | ||
case LogLevel.Error: | ||
case LogLevel.Critical: | ||
if (exception == null) | ||
{ | ||
Log.Error(messageTemplate); | ||
} | ||
else | ||
{ | ||
Log.Error(exception, messageTemplate); | ||
} | ||
|
||
break; | ||
case LogLevel.Trace: | ||
case LogLevel.Debug: | ||
default: | ||
Log.Debug(messageTemplate); | ||
break; | ||
} | ||
}; | ||
} | ||
|
||
public static IHostBuilder UseApolloForHostBuilder(this IHostBuilder hostBuilder) | ||
{ | ||
return hostBuilder | ||
.ConfigureAppConfiguration((_, builder) => { builder.AddJsonFile("appsettings.apollo.json"); }) | ||
.ConfigureAppConfiguration((_, builder) => | ||
{ | ||
if (!builder.Build().GetValue<bool>("IsApolloEnabled", false)) | ||
{ | ||
return; | ||
} | ||
//To display the Apollo console logs | ||
#if DEBUG | ||
LogManager.UseConsoleLogging(Com.Ctrip.Framework.Apollo.Logging.LogLevel.Trace); | ||
#endif | ||
builder.AddApollo(builder.Build().GetSection("apollo")); | ||
}) | ||
.ConfigureGloballySharedLog(true); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{ | ||
"apollo": { | ||
"AppId": "Portkey.Test4V2", | ||
"ConfigServer": [ | ||
"http://localhost:8080" | ||
], | ||
"Namespaces": [ | ||
"push.httpapi.appsettings.json" | ||
], | ||
"Env": "DEV", | ||
"RefreshInterval": 60000, | ||
"Timeout": 5000, | ||
"StartupTimeout": 30000 | ||
}, | ||
"IsApolloEnabled": false | ||
} |