Skip to content

Commit 6726d00

Browse files
Merge pull request #42 from myvas/remove-serilog
Remove Serilog
2 parents 823590a + 88904db commit 6726d00

File tree

6 files changed

+57
-19
lines changed

6 files changed

+57
-19
lines changed

src/Core/Demo.Data.Host/Views/Roles/Index.cshtml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
@model IEnumerable<Demo.Models.AppRole>
1+
@model IList<Demo.Models.AppRole>
22

33
@{
44
ViewData["Title"] = "Index";

src/MvcDemo/Controllers/WeixinController.cs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,24 @@ public class WeixinController : Controller
1616
private readonly IWeixinUserApi _api;
1717
private readonly IWeixinCustomerSupportApi _csApi;
1818
private readonly IWeixinSubscriberStore _subscriberStore;
19-
private readonly IWeixinResponseMessageStore<WeixinResponseMessageEntity> _messageStore;
19+
private readonly IWeixinResponseMessageStore<WeixinResponseMessageEntity> _responseStore;
20+
private readonly IWeixinReceivedMessageStore<WeixinReceivedMessageEntity> _messageStore;
2021
private readonly IWeixinReceivedEventStore<WeixinReceivedEventEntity> _eventStore;
2122

2223
public WeixinController(
2324
ILogger<WeixinController> logger,
2425
IWeixinUserApi api,
2526
IWeixinCustomerSupportApi csApi,
2627
IWeixinSubscriberStore subscriberStore,
27-
IWeixinResponseMessageStore<WeixinResponseMessageEntity> messageStore,
28+
IWeixinResponseMessageStore<WeixinResponseMessageEntity> responseStore,
29+
IWeixinReceivedMessageStore<WeixinReceivedMessageEntity> messageStore,
2830
IWeixinReceivedEventStore<WeixinReceivedEventEntity> eventStore)
2931
{
3032
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
3133
_api = api;
3234
_csApi = csApi;
3335
_subscriberStore = subscriberStore ?? throw new ArgumentNullException(nameof(subscriberStore));
36+
_responseStore=responseStore ?? throw new ArgumentNullException(nameof(responseStore));
3437
_messageStore = messageStore ?? throw new ArgumentNullException(nameof(messageStore));
3538
_eventStore = eventStore ?? throw new ArgumentNullException(nameof(eventStore));
3639
}
@@ -61,6 +64,7 @@ public async Task<IActionResult> Subscribers(int? n)
6164
var subscribers = await _subscriberStore.GetItemsAsync(pageSize, pageIndex);
6265
_logger.LogDebug($"微信订阅者在数据库中共{totalRecords}条记录。");
6366
vm.Item = subscribers;
67+
vm.ReturnUrl=Url.Action(nameof(Subscribers), new { n });
6468

6569
return View(vm);
6670
}
@@ -81,9 +85,10 @@ public async Task<IActionResult> ReceivedText(int? n)
8185

8286
var pageIndex = n.Value - 1;
8387

84-
var items = await _messageStore.GetItemsAsync(pageSize, pageIndex);
88+
vm.Item = await _messageStore.GetItemsAsync(pageSize, pageIndex);
89+
vm.ReturnUrl = Url.Action(nameof(ReceivedText), new { n });
8590
_logger.LogDebug($"微信文本消息在数据库中共{totalRecords}条记录。");
86-
return View(items);
91+
return View(vm);
8792
}
8893

8994
public async Task<IActionResult> SendWeixin(string openId)
@@ -95,7 +100,7 @@ public async Task<IActionResult> SendWeixin(string openId)
95100

96101
var vm = new SendWeixinViewModel
97102
{
98-
Received = await _messageStore.Items.Where(x => x.ToUserName == openId).ToListAsync(),
103+
Responsed = await _responseStore.Items.Where(x => x.ToUserName == openId).ToListAsync(),
99104
OpenId = openId
100105
};
101106
return View(vm);

src/MvcDemo/Demo.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
<PackageReference Include="Myvas.AspNetCore.TencentSms" Version="9.0.*" />
2929
<PackageReference Include="Myvas.AspNetCore.ViewDivert" Version="9.0.*" />
3030
<PackageReference Include="Myvas.AspNetCore.Weixin" Version="9.0.0-rc.6" />
31-
<PackageReference Include="Serilog.AspNetCore" Version="9.0.*" />
3231
</ItemGroup>
3332

3433
</Project>

src/MvcDemo/HostExtensions.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1+
using System;
2+
using System.Diagnostics;
3+
using System.Reflection;
14
using Demo.Data;
25
using Microsoft.AspNetCore.Builder;
36
using Microsoft.AspNetCore.Identity;
47
using Microsoft.EntityFrameworkCore;
58
using Microsoft.Extensions.Configuration;
69
using Microsoft.Extensions.DependencyInjection;
710
using Microsoft.Extensions.Hosting;
11+
using Microsoft.Extensions.Logging;
812
using Myvas.AspNetCore.Authentication;
913
using Myvas.AspNetCore.Weixin;
1014

@@ -14,6 +18,21 @@ public static class HostExtensions
1418
{
1519
public static WebApplicationBuilder ConfigureServices(this WebApplicationBuilder builder)
1620
{
21+
Debug.WriteLine("Create a temporary logger for me.");
22+
// Build a temporary logger for me.
23+
using var loggerFactory = LoggerFactory.Create(logging =>
24+
{
25+
logging.AddConsole();
26+
#if DEBUG
27+
logging.SetMinimumLevel(LogLevel.Trace);
28+
#else
29+
logging.SetMinimumLevel(LogLevel.Information);
30+
#endif
31+
});
32+
var logger = loggerFactory.CreateLogger<WebApplicationBuilder>();
33+
logger.LogDebug($"{MethodBase.GetCurrentMethod().Name}...");
34+
logger.LogInformation("Environment=" + builder.Environment.EnvironmentName);
35+
1736
var Configuration = builder.Configuration;
1837

1938
builder.Services.AddControllersWithViews();
@@ -47,6 +66,10 @@ public static WebApplicationBuilder ConfigureServices(this WebApplicationBuilder
4766
o.AccessDeniedPath = "/Identity/Account/AccessDenied";
4867
});
4968

69+
logger?.LogInformation("WeixinOpen:AppId=" + Configuration["WeixinOpen:AppId"]);
70+
logger?.LogInformation("WeixinAuth:AppId=" + Configuration["WeixinAuth:AppId"]);
71+
logger?.LogInformation("QQConnect:AppId=" + Configuration["QQConnect:AppId"]);
72+
5073
builder.Services.AddAuthentication()
5174
.AddWeixinOpen(o =>
5275
{
@@ -74,6 +97,8 @@ public static WebApplicationBuilder ConfigureServices(this WebApplicationBuilder
7497
QQConnectScopes.do_like);
7598
});
7699

100+
logger?.LogInformation("TencensSms:SdkAppId=" + Configuration["TencentSms:SdkAppId"]);
101+
77102
builder.Services.AddTencentSms(o =>
78103
{
79104
o.SdkAppId = Configuration["TencentSms:SdkAppId"];
@@ -82,6 +107,8 @@ public static WebApplicationBuilder ConfigureServices(this WebApplicationBuilder
82107

83108
builder.Services.AddViewDivert();
84109

110+
logger?.LogInformation("Weixin:AppId=" + Configuration["Weixin:AppId"]);
111+
85112
builder.Services.AddWeixin(o =>
86113
{
87114
o.AppId = Configuration["Weixin:AppId"];
@@ -111,6 +138,10 @@ public static WebApplicationBuilder ConfigureServices(this WebApplicationBuilder
111138

112139
public static WebApplication ConfigurePipeline(this WebApplication app)
113140
{
141+
var logger = app.Logger;
142+
logger.LogTrace($"{MethodBase.GetCurrentMethod().Name}...");
143+
logger.LogInformation($"Environment={app.Environment.EnvironmentName}");
144+
114145
var env = app.Environment;
115146
if (env.IsDevelopment())
116147
{
@@ -124,6 +155,8 @@ public static WebApplication ConfigurePipeline(this WebApplication app)
124155
}
125156
//app.UseHttpsRedirection();
126157
app.UseStaticFiles();
158+
159+
logger.LogTrace("UseWeixinSite...");
127160
app.UseWeixinSite();
128161

129162
app.UseRouting();

src/MvcDemo/Models/WeixinViewModels/SendWeixinViewModel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace Demo.Models
77
{
88
public class SendWeixinViewModel
99
{
10-
public IEnumerable<WeixinResponseMessageEntity> Received { get; set; }
10+
public IList<WeixinResponseMessageEntity> Responsed { get; set; }
1111

1212
[Required]
1313
public string OpenId { get; set; }

src/MvcDemo/Program.cs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,27 @@
22
using Demo.Data;
33
using Microsoft.AspNetCore.Builder;
44
using Microsoft.Extensions.Hosting;
5-
using Serilog;
5+
using Microsoft.Extensions.Logging;
66
using System;
77
using System.Reflection;
88

9-
Log.Logger = new LoggerConfiguration()
10-
.WriteTo.Console()
11-
.CreateLogger();
12-
139
var assembly = typeof(Program).Assembly;
1410
var assemblyName = assembly.GetName().Name;
1511
var assemblyVersion = assembly.GetName().Version?.ToString()
1612
?? assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>()?.InformationalVersion;
17-
Log.Information($"{assemblyName} v{assemblyVersion} starting up...");
13+
Console.WriteLine($"{assemblyName} v{assemblyVersion} starting up...");
1814

1915
try
2016
{
2117
var builder = WebApplication.CreateBuilder(args);
2218

19+
// Set default logging level.
20+
#if DEBUG
21+
builder.Logging.SetMinimumLevel(LogLevel.Trace);
22+
#else
23+
builder.Logging.SetMinimumLevel(LogLevel.Information);
24+
#endif
25+
2326
var app = builder.ConfigureServices().Build()
2427
.MigrateDatabase()
2528
.SeedDatabase("demo", "demo@myvas.com")
@@ -29,12 +32,10 @@
2932
}
3033
catch (Exception ex)
3134
{
32-
// Any unhandled exception during start-up will be caught and flushed to
33-
// our log file or centralized log server
34-
Log.Fatal(ex, "Host terminated for an unhandled exception occured.");
35+
Console.WriteLine($"{assemblyName} v{assemblyVersion} terminated for an unhandled exception occured.");
36+
Console.WriteLine(ex);
3537
}
3638
finally
3739
{
38-
Log.Information($"{assemblyName} v{assemblyVersion} shutdown.");
39-
Log.CloseAndFlush();
40+
Console.WriteLine($"{assemblyName} v{assemblyVersion} shutdown.");
4041
}

0 commit comments

Comments
 (0)