diff --git a/src/Core/Demo.Data.Host/Views/Roles/Index.cshtml b/src/Core/Demo.Data.Host/Views/Roles/Index.cshtml index 0b73084..46e8e01 100644 --- a/src/Core/Demo.Data.Host/Views/Roles/Index.cshtml +++ b/src/Core/Demo.Data.Host/Views/Roles/Index.cshtml @@ -1,4 +1,4 @@ -@model IEnumerable +@model IList @{ ViewData["Title"] = "Index"; diff --git a/src/MvcDemo/Controllers/WeixinController.cs b/src/MvcDemo/Controllers/WeixinController.cs index faf2c84..4c86c67 100644 --- a/src/MvcDemo/Controllers/WeixinController.cs +++ b/src/MvcDemo/Controllers/WeixinController.cs @@ -16,7 +16,8 @@ public class WeixinController : Controller private readonly IWeixinUserApi _api; private readonly IWeixinCustomerSupportApi _csApi; private readonly IWeixinSubscriberStore _subscriberStore; - private readonly IWeixinResponseMessageStore _messageStore; + private readonly IWeixinResponseMessageStore _responseStore; + private readonly IWeixinReceivedMessageStore _messageStore; private readonly IWeixinReceivedEventStore _eventStore; public WeixinController( @@ -24,13 +25,15 @@ public WeixinController( IWeixinUserApi api, IWeixinCustomerSupportApi csApi, IWeixinSubscriberStore subscriberStore, - IWeixinResponseMessageStore messageStore, + IWeixinResponseMessageStore responseStore, + IWeixinReceivedMessageStore messageStore, IWeixinReceivedEventStore eventStore) { _logger = logger ?? throw new ArgumentNullException(nameof(logger)); _api = api; _csApi = csApi; _subscriberStore = subscriberStore ?? throw new ArgumentNullException(nameof(subscriberStore)); + _responseStore=responseStore ?? throw new ArgumentNullException(nameof(responseStore)); _messageStore = messageStore ?? throw new ArgumentNullException(nameof(messageStore)); _eventStore = eventStore ?? throw new ArgumentNullException(nameof(eventStore)); } @@ -61,6 +64,7 @@ public async Task Subscribers(int? n) var subscribers = await _subscriberStore.GetItemsAsync(pageSize, pageIndex); _logger.LogDebug($"微信订阅者在数据库中共{totalRecords}条记录。"); vm.Item = subscribers; + vm.ReturnUrl=Url.Action(nameof(Subscribers), new { n }); return View(vm); } @@ -81,9 +85,10 @@ public async Task ReceivedText(int? n) var pageIndex = n.Value - 1; - var items = await _messageStore.GetItemsAsync(pageSize, pageIndex); + vm.Item = await _messageStore.GetItemsAsync(pageSize, pageIndex); + vm.ReturnUrl = Url.Action(nameof(ReceivedText), new { n }); _logger.LogDebug($"微信文本消息在数据库中共{totalRecords}条记录。"); - return View(items); + return View(vm); } public async Task SendWeixin(string openId) @@ -95,7 +100,7 @@ public async Task SendWeixin(string openId) var vm = new SendWeixinViewModel { - Received = await _messageStore.Items.Where(x => x.ToUserName == openId).ToListAsync(), + Responsed = await _responseStore.Items.Where(x => x.ToUserName == openId).ToListAsync(), OpenId = openId }; return View(vm); diff --git a/src/MvcDemo/Demo.csproj b/src/MvcDemo/Demo.csproj index edda96a..532399c 100644 --- a/src/MvcDemo/Demo.csproj +++ b/src/MvcDemo/Demo.csproj @@ -28,7 +28,6 @@ - diff --git a/src/MvcDemo/HostExtensions.cs b/src/MvcDemo/HostExtensions.cs index 81ac6ac..f696ace 100644 --- a/src/MvcDemo/HostExtensions.cs +++ b/src/MvcDemo/HostExtensions.cs @@ -1,3 +1,6 @@ +using System; +using System.Diagnostics; +using System.Reflection; using Demo.Data; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Identity; @@ -5,6 +8,7 @@ using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Logging; using Myvas.AspNetCore.Authentication; using Myvas.AspNetCore.Weixin; @@ -14,6 +18,21 @@ public static class HostExtensions { public static WebApplicationBuilder ConfigureServices(this WebApplicationBuilder builder) { + Debug.WriteLine("Create a temporary logger for me."); + // Build a temporary logger for me. + using var loggerFactory = LoggerFactory.Create(logging => + { + logging.AddConsole(); +#if DEBUG + logging.SetMinimumLevel(LogLevel.Trace); +#else + logging.SetMinimumLevel(LogLevel.Information); +#endif + }); + var logger = loggerFactory.CreateLogger(); + logger.LogDebug($"{MethodBase.GetCurrentMethod().Name}..."); + logger.LogInformation("Environment=" + builder.Environment.EnvironmentName); + var Configuration = builder.Configuration; builder.Services.AddControllersWithViews(); @@ -47,6 +66,10 @@ public static WebApplicationBuilder ConfigureServices(this WebApplicationBuilder o.AccessDeniedPath = "/Identity/Account/AccessDenied"; }); + logger?.LogInformation("WeixinOpen:AppId=" + Configuration["WeixinOpen:AppId"]); + logger?.LogInformation("WeixinAuth:AppId=" + Configuration["WeixinAuth:AppId"]); + logger?.LogInformation("QQConnect:AppId=" + Configuration["QQConnect:AppId"]); + builder.Services.AddAuthentication() .AddWeixinOpen(o => { @@ -74,6 +97,8 @@ public static WebApplicationBuilder ConfigureServices(this WebApplicationBuilder QQConnectScopes.do_like); }); + logger?.LogInformation("TencensSms:SdkAppId=" + Configuration["TencentSms:SdkAppId"]); + builder.Services.AddTencentSms(o => { o.SdkAppId = Configuration["TencentSms:SdkAppId"]; @@ -82,6 +107,8 @@ public static WebApplicationBuilder ConfigureServices(this WebApplicationBuilder builder.Services.AddViewDivert(); + logger?.LogInformation("Weixin:AppId=" + Configuration["Weixin:AppId"]); + builder.Services.AddWeixin(o => { o.AppId = Configuration["Weixin:AppId"]; @@ -111,6 +138,10 @@ public static WebApplicationBuilder ConfigureServices(this WebApplicationBuilder public static WebApplication ConfigurePipeline(this WebApplication app) { + var logger = app.Logger; + logger.LogTrace($"{MethodBase.GetCurrentMethod().Name}..."); + logger.LogInformation($"Environment={app.Environment.EnvironmentName}"); + var env = app.Environment; if (env.IsDevelopment()) { @@ -124,6 +155,8 @@ public static WebApplication ConfigurePipeline(this WebApplication app) } //app.UseHttpsRedirection(); app.UseStaticFiles(); + + logger.LogTrace("UseWeixinSite..."); app.UseWeixinSite(); app.UseRouting(); diff --git a/src/MvcDemo/Models/WeixinViewModels/SendWeixinViewModel.cs b/src/MvcDemo/Models/WeixinViewModels/SendWeixinViewModel.cs index a9c189d..ef03a2e 100644 --- a/src/MvcDemo/Models/WeixinViewModels/SendWeixinViewModel.cs +++ b/src/MvcDemo/Models/WeixinViewModels/SendWeixinViewModel.cs @@ -7,7 +7,7 @@ namespace Demo.Models { public class SendWeixinViewModel { - public IEnumerable Received { get; set; } + public IList Responsed { get; set; } [Required] public string OpenId { get; set; } diff --git a/src/MvcDemo/Program.cs b/src/MvcDemo/Program.cs index 548c2ec..4e74854 100644 --- a/src/MvcDemo/Program.cs +++ b/src/MvcDemo/Program.cs @@ -2,24 +2,27 @@ using Demo.Data; using Microsoft.AspNetCore.Builder; using Microsoft.Extensions.Hosting; -using Serilog; +using Microsoft.Extensions.Logging; using System; using System.Reflection; -Log.Logger = new LoggerConfiguration() - .WriteTo.Console() - .CreateLogger(); - var assembly = typeof(Program).Assembly; var assemblyName = assembly.GetName().Name; var assemblyVersion = assembly.GetName().Version?.ToString() ?? assembly.GetCustomAttribute()?.InformationalVersion; -Log.Information($"{assemblyName} v{assemblyVersion} starting up..."); +Console.WriteLine($"{assemblyName} v{assemblyVersion} starting up..."); try { var builder = WebApplication.CreateBuilder(args); + // Set default logging level. +#if DEBUG + builder.Logging.SetMinimumLevel(LogLevel.Trace); +#else + builder.Logging.SetMinimumLevel(LogLevel.Information); +#endif + var app = builder.ConfigureServices().Build() .MigrateDatabase() .SeedDatabase("demo", "demo@myvas.com") @@ -29,12 +32,10 @@ } catch (Exception ex) { - // Any unhandled exception during start-up will be caught and flushed to - // our log file or centralized log server - Log.Fatal(ex, "Host terminated for an unhandled exception occured."); + Console.WriteLine($"{assemblyName} v{assemblyVersion} terminated for an unhandled exception occured."); + Console.WriteLine(ex); } finally { - Log.Information($"{assemblyName} v{assemblyVersion} shutdown."); - Log.CloseAndFlush(); + Console.WriteLine($"{assemblyName} v{assemblyVersion} shutdown."); }