Skip to content

Commit

Permalink
Merge pull request #11 from Portkey-Wallet/hotfix/im-apollo-integration
Browse files Browse the repository at this point in the history
apollo integration
  • Loading branch information
chaoxkang authored May 15, 2024
2 parents 87807e3 + c74ddee commit 9b5cc0c
Show file tree
Hide file tree
Showing 7 changed files with 198 additions and 24 deletions.
32 changes: 32 additions & 0 deletions src/MessagePush.Application/Common/ConfigurationProvidersHelper.cs
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.");
}
}
}
22 changes: 22 additions & 0 deletions src/MessagePush.Application/Common/LogHelper.cs
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();
}
}
5 changes: 5 additions & 0 deletions src/MessagePush.Application/MessagePush.Application.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@
<PackageReference Include="Volo.Abp.FeatureManagement.Application" Version="7.0.0" />
<PackageReference Include="Volo.Abp.SettingManagement.Application" Version="7.0.0" />
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="6.25.1" />
<PackageReference Include="Com.Ctrip.Framework.Apollo.Configuration" Version="2.10.1" />
<PackageReference Include="Volo.Abp.AspNetCore.Serilog" Version="7.0.0" />
<PackageReference Include="Serilog.AspNetCore" Version="5.0.0" />
<PackageReference Include="Serilog.Sinks.Async" Version="1.5.0" />
<PackageReference Include="Serilog.Sinks.RollingFile" Version="3.3.0" />
</ItemGroup>

<ItemGroup>
Expand Down
117 changes: 117 additions & 0 deletions src/MessagePush.Application/MessagePushHostBuilderExtensions.cs
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);
}
}
9 changes: 3 additions & 6 deletions src/MessagePush.HttpApi.Host/MessagePushHttpApiHostModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using Google.Apis.Auth.OAuth2;
using Medallion.Threading;
using Medallion.Threading.Redis;
using MessagePush.Grains;
using MessagePush.Common;
using MessagePush.MongoDb;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Builder;
Expand All @@ -15,11 +15,7 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.OpenApi.Models;
using Orleans;
using Orleans.Configuration;
using Orleans.Providers.MongoDB.Configuration;
using StackExchange.Redis;
using Volo.Abp;
using Volo.Abp.AspNetCore.Mvc;
Expand All @@ -33,7 +29,6 @@
using Volo.Abp.Modularity;
using Volo.Abp.OpenIddict.Tokens;
using Volo.Abp.Swashbuckle;
using Volo.Abp.Threading;

namespace MessagePush;

Expand Down Expand Up @@ -206,6 +201,8 @@ public override void OnApplicationInitialization(ApplicationInitializationContex
{
Credential = GoogleCredential.FromFile("firebase.config.json")
});

ConfigurationProvidersHelper.DisplayConfigurationProviders(context);
}

private void ConfigureTokenCleanupService()
Expand Down
21 changes: 3 additions & 18 deletions src/MessagePush.HttpApi.Host/Program.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using System;
using System.Threading.Tasks;
using MessagePush.Common;
using MessagePush.ScheduledTasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Serilog;
Expand All @@ -14,29 +14,14 @@ public class Program
{
public async static Task<int> Main(string[] args)
{
var configuration = new ConfigurationBuilder()
.AddJsonFile("appsettings.json")
.Build();
Log.Logger = new LoggerConfiguration()
#if DEBUG
.MinimumLevel.Debug()
#else
.MinimumLevel.Information()
#endif
.MinimumLevel.Override("Microsoft", LogEventLevel.Information)
.Enrich.FromLogContext()
.ReadFrom.Configuration(configuration)

#if DEBUG
.WriteTo.Async(c => c.Console())
#endif
.CreateLogger();
Log.Logger = LogHelper.CreateLogger(LogEventLevel.Debug);

try
{
Log.Information("Starting MessagePush.HttpApi.Host.");
var builder = WebApplication.CreateBuilder(args);
builder.Host.AddAppSettingsSecretsJson()
.UseApolloForConfigureHostBuilder()
.UseAutofac()
.UseSerilog();

Expand Down
16 changes: 16 additions & 0 deletions src/MessagePush.HttpApi.Host/appsettings.apollo.json
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
}

0 comments on commit 9b5cc0c

Please sign in to comment.