GeezerVison is showing github.com — 加入log4net日志处理类 · feier09/Emma.Blog@42b262b
File treeExpand file treeCollapse file tree Open diff view settings Properties/PublishProfiles
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboard +11Lines changed: 11 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +<Project Sdk="Microsoft.NET.Sdk"> | | 2 | + | | 3 | + <PropertyGroup> | | 4 | + <TargetFramework>netcoreapp2.0</TargetFramework> | | 5 | + </PropertyGroup> | | 6 | + | | 7 | + <ItemGroup> | | 8 | + <PackageReference Include="log4net" Version="2.0.8" /> | | 9 | + </ItemGroup> | | 10 | + | | 11 | +</Project> |
Collapse file Copy file name to clipboard +49Lines changed: 49 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +using System; | | 2 | +using System.Collections.Concurrent; | | 3 | +using System.IO; | | 4 | +using log4net; | | 5 | +using log4net.Config; | | 6 | +using log4net.Repository; | | 7 | + | | 8 | + | | 9 | +namespace Emma.Blog.Common | | 10 | +{ | | 11 | + | | 12 | + public class LogHelper | | 13 | + { | | 14 | + private static ILog logger; | | 15 | + static LogHelper() | | 16 | + { | | 17 | + | | 18 | + ILoggerRepository repository = LogManager.CreateRepository("NETCoreRepository"); | | 19 | + XmlConfigurator.Configure(repository, new FileInfo("log4net.config")); | | 20 | + logger = LogManager.GetLogger(repository.Name, "NETCorelog4net"); | | 21 | + } | | 22 | + | | 23 | + | | 24 | + public static void Error(object message, Exception ex) | | 25 | + { | | 26 | + logger.Error(message, ex); | | 27 | + } | | 28 | + | | 29 | + | | 30 | + public static void Error(object message) | | 31 | + { | | 32 | + logger.Error(message); | | 33 | + } | | 34 | + | | 35 | + public static void Info(object message, Exception ex) | | 36 | + { | | 37 | + logger.Error(message, ex); | | 38 | + } | | 39 | + | | 40 | + public static void Info(object message) | | 41 | + { | | 42 | + logger.Info(message); | | 43 | + } | | 44 | + | | 45 | + | | 46 | + | | 47 | + | | 48 | + } | | 49 | +} |
Collapse file Copy file name to clipboardExpand all lines: Emma.Blog.Service/MyHttpContext.cs -5Lines changed: 0 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -8,11 +8,6 @@ namespace Emma.Blog.Service | 8 | 8 |
| 9 | 9 | public static class MyHttpContext | 10 | 10 | { | 11 | | - public static IServiceProvider ServiceProvider; | 12 | | - | 13 | | - static MyHttpContext() | 14 | | - { } | 15 | | - | 16 | 11 |
| 17 | 12 | public static HttpContext Current | 18 | 13 | { | |
Collapse file Copy file name to clipboard +21Lines changed: 21 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +using System; | | 2 | +using System.Collections.Generic; | | 3 | +using System.Text; | | 4 | + | | 5 | +namespace Emma.Blog.Service | | 6 | +{ | | 7 | + public static class ServiceProvider | | 8 | + { | | 9 | + public static IServiceProvider Provider; | | 10 | + | | 11 | + static ServiceProvider() | | 12 | + { | | 13 | + | | 14 | + } | | 15 | + public static object GetService(Type serviceType) | | 16 | + { | | 17 | + return Provider.GetService(serviceType: serviceType); | | 18 | + | | 19 | + } | | 20 | + } | | 21 | +} |
Collapse file Copy file name to clipboardExpand all lines: Emma.Blog.Web/Controllers/HomeController.cs +12-10Lines changed: 12 additions & 10 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | | -using System; | 2 | | -using System.Collections.Generic; | 3 | | -using System.Diagnostics; | 4 | | -using System.Linq; | 5 | | -using System.Threading.Tasks; | 6 | | -using Emma.Blog.Data; | 7 | | -using Emma.Blog.Data.Models; | 8 | | -using Emma.Blog.Service; | 9 | | -using Microsoft.AspNetCore.Mvc; | | 1 | +using Emma.Blog.Data.Models; | | 2 | +using Emma.Blog.Service.Account; | 10 | 3 | using Emma.Blog.Web.Models; | 11 | 4 | using Microsoft.AspNetCore.Authorization; | | 5 | +using Microsoft.AspNetCore.Mvc; | | 6 | +using System.Diagnostics; | 12 | 7 |
| 13 | 8 | namespace Emma.Blog.Web.Controllers | 14 | 9 | { | 15 | 10 | public class HomeController : Controller | 16 | 11 | { | 17 | | - | | 12 | + | 18 | 13 | public IActionResult Index() | 19 | 14 | { | | 15 | + | | 16 | + UserService service = new UserService(); | | 17 | + | | 18 | + var claimUser = service.Register(new User() | | 19 | + { | | 20 | + NickName = "mysql" | | 21 | + }); | 20 | 22 | return View(); | 21 | 23 | } | 22 | 24 |
| |
Collapse file Copy file name to clipboardExpand all lines: Emma.Blog.Web/Controllers/UserController.cs +15-13Lines changed: 15 additions & 13 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | | -using Emma.Blog.Service.Auth; | | 1 | +using Emma.Blog.Data.Models; | | 2 | +using Emma.Blog.Service.Account; | | 3 | +using Emma.Blog.Service.Auth; | 2 | 4 | using Microsoft.AspNetCore.Authentication; | 3 | 5 | using Microsoft.AspNetCore.Authentication.Cookies; | 4 | 6 | using Microsoft.AspNetCore.Authorization; | 5 | 7 | using Microsoft.AspNetCore.Mvc; | 6 | 8 | using Microsoft.Extensions.Options; | 7 | 9 | using System; | 8 | | -using System.Collections.Generic; | 9 | 10 | using System.Security.Claims; | 10 | | -using Emma.Blog.Data.Models; | 11 | | -using Emma.Blog.Service.Account; | 12 | 11 |
| 13 | 12 |
| 14 | 13 | namespace Emma.Blog.Web.Controllers | @@ -39,17 +38,20 @@ public IActionResult Login() | 39 | 38 | [HttpPost] | 40 | 39 | public IActionResult Login(User user) | 41 | 40 | { | 42 | | - UserService service = new UserService(); | 43 | | - var claimUser = service.Register(user); | | 41 | + | | 42 | + UserService service = new UserService(); | | 43 | + var claimUser = service.Register(user); | 44 | 44 |
| 45 | | - var claimsPrincipal = new ClaimsPrincipal(new ClaimsIdentity(claimUser.GetClaims(), CookieAuthenticationDefaults.AuthenticationScheme)); | 46 | | - HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, claimsPrincipal, new AuthenticationProperties() | 47 | | - { | 48 | | - IsPersistent = true, | 49 | | - ExpiresUtc = DateTime.UtcNow.AddSeconds(_jwtSettings.Expires) | 50 | | - }); | | 45 | + var claimsPrincipal = new ClaimsPrincipal(new ClaimsIdentity(claimUser.GetClaims(), CookieAuthenticationDefaults.AuthenticationScheme)); | | 46 | + HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, claimsPrincipal, new AuthenticationProperties() | | 47 | + { | | 48 | + IsPersistent = true, | | 49 | + ExpiresUtc = DateTime.UtcNow.AddSeconds(_jwtSettings.Expires) | | 50 | + }); | 51 | 51 |
| 52 | | - return RedirectToAction("Index","Home"); | | 52 | + return RedirectToAction("Index", "Home"); | | 53 | + | | 54 | + | 53 | 55 |
| 54 | 56 | } | 55 | 57 |
| |
Collapse file Copy file name to clipboardExpand all lines: Emma.Blog.Web/Emma.Blog.Web.csproj +2Lines changed: 2 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 8 | 8 | <PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.0" /> | 9 | 9 | <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.0.0" /> | 10 | 10 | <PackageReference Include="Newtonsoft.Json" Version="10.0.3" /> | | 11 | + | 11 | 12 | </ItemGroup> | 12 | 13 |
| 13 | 14 | <ItemGroup> | | 16 | 17 | </ItemGroup> | 17 | 18 |
| 18 | 19 | <ItemGroup> | | 20 | + <ProjectReference Include="..\Emma.Blog.Common\Emma.Blog.Common.csproj" /> | 19 | 21 | <ProjectReference Include="..\Emma.Blog.Service\Emma.Blog.Service.csproj" /> | 20 | 22 | </ItemGroup> | 21 | 23 |
| |
Collapse file Copy file name to clipboardExpand all lines: Emma.Blog.Web/Extensions/JwtDataFormat.cs -1Lines changed: 0 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 3 | 3 | using System.Security.Claims; | 4 | 4 | using System.Text; | 5 | 5 | using Emma.Blog.Service.Auth; | 6 | | -using Emma.Blog.Web.Models; | 7 | 6 | using Microsoft.AspNetCore.Authentication; | 8 | 7 | using Microsoft.AspNetCore.Authentication.Cookies; | 9 | 8 | using Microsoft.IdentityModel.Tokens; | |
Collapse file Copy file name to clipboardExpand all lines: Emma.Blog.Web/MyCookieManager.cs +3-3Lines changed: 3 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -14,9 +14,9 @@ public void AppendResponseCookie(HttpContext context, string key, string value, | 14 | 14 |
| 15 | 15 | foreach (var item in context.User.Claims) | 16 | 16 | { | 17 | | - Console.WriteLine("==================claimstype:"+item.Type+",claimsValue:"+item.Value); | | 17 | + Console.WriteLine("==================claimstype:" + item.Type + ",claimsValue:" + item.Value); | 18 | 18 | } | 19 | | - Console.WriteLine("AppendResponseCookie===================key:" + key+",value:"+value); | | 19 | + Console.WriteLine("AppendResponseCookie===================key:" + key + ",value:" + value); | 20 | 20 |
| 21 | 21 | context.Response.Cookies.Append(key, value); | 22 | 22 | } | @@ -29,7 +29,7 @@ public void DeleteCookie(HttpContext context, string key, CookieOptions options) | 29 | 29 | public string GetRequestCookie(HttpContext context, string key) | 30 | 30 | { | 31 | 31 | var cookieValue = context.Request.Cookies[key]; | 32 | | - Console.WriteLine("GetRequestCookie==================="+key+",value="+cookieValue); | | 32 | + Console.WriteLine("GetRequestCookie===================" + key + ",value=" + cookieValue); | 33 | 33 | return cookieValue; | 34 | 34 | } | 35 | 35 | } | |
Collapse file Copy file name to clipboard +10-11Lines changed: 10 additions & 11 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | | -using System; | 2 | | -using System.Collections.Generic; | 3 | | -using System.IO; | 4 | | -using System.Linq; | 5 | | -using System.Threading.Tasks; | 6 | | -using Emma.Blog.Data; | 7 | | -using Microsoft.AspNetCore; | | 1 | +using Microsoft.AspNetCore; | 8 | 2 | using Microsoft.AspNetCore.Hosting; | 9 | | -using Microsoft.Extensions.Configuration; | 10 | | -using Microsoft.Extensions.DependencyInjection; | 11 | 3 | using Microsoft.Extensions.Logging; | 12 | 4 |
| 13 | 5 | namespace Emma.Blog.Web | @@ -16,15 +8,22 @@ public class Program | 16 | 8 | { | 17 | 9 | public static void Main(string[] args) | 18 | 10 | { | | 11 | + | 19 | 12 | var host = BuildWebHost(args); | 20 | 13 |
| 21 | 14 | host.Run(); | 22 | | - | | 15 | + | 23 | 16 | } | 24 | 17 |
| 25 | 18 | public static IWebHost BuildWebHost(string[] args) => | 26 | 19 | WebHost.CreateDefaultBuilder(args) | 27 | 20 | .UseStartup<Startup>() | 28 | | - .Build(); | | 21 | + .UseKestrel() | | 22 | + | | 23 | + .UseStartup<Startup>() | | 24 | + .Build(); | | 25 | + | | 26 | + | | 27 | + | 29 | 28 | } | 30 | 29 | } |
|