sysLogService.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. using Bowin.Common.Utility;
  2. using YLShipBuildLandMap.Entity;
  3. using Microsoft.AspNetCore.Http;
  4. using Newtonsoft.Json;
  5. using Newtonsoft.Json.Linq;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Security.Claims;
  9. using System.Text;
  10. namespace YLShipBuildLandMap.Services.SystemSetting
  11. {
  12. public class SysLogService : Bowin.Common.Log.IDbLog
  13. {
  14. private YLShipBuildLandMapContext DbContext { get; set; }
  15. public SysLogService(YLShipBuildLandMapContext dbContext) {
  16. this.DbContext = dbContext;
  17. }
  18. public bool DbWrite(Exception ex,HttpContext httpContext)
  19. {
  20. try
  21. {
  22. SysLog log = new SysLog();
  23. string ip = httpContext.Connection.RemoteIpAddress.ToString();
  24. log.LogId = Guid.NewGuid();
  25. log.LogTime = DateTime.Now;
  26. log.ClientIp = ip;
  27. if (httpContext.User != null)
  28. {
  29. var roleClaim = httpContext.User.FindFirst(ClaimTypes.Name);
  30. log.LogUser = roleClaim != null ? roleClaim.Value : "";
  31. }
  32. log.LogTitle = httpContext.Request.Path.HasValue ? httpContext.Request.Path.Value : "";
  33. log.LogDescribe = "filterError";
  34. log.LogType = 2;
  35. log.LogText = JsonConvert.SerializeObject(new { ex.Message,ex.StackTrace }); ;
  36. this.DbContext.SysLog.Add(log);
  37. this.DbContext.SaveChanges();
  38. return true;
  39. }
  40. catch {
  41. return false;
  42. }
  43. }
  44. public bool DbWrite(JObject jobject)
  45. {
  46. try
  47. {
  48. var httpContext= HttpHelper.Current;
  49. SysLog log = new SysLog();
  50. string ip = httpContext.Connection.RemoteIpAddress.ToString();
  51. log.LogId = Guid.NewGuid();
  52. log.LogTime = DateTime.Now;
  53. log.ClientIp = ip;
  54. if (httpContext.User != null)
  55. {
  56. var roleClaim = httpContext.User.FindFirst(ClaimTypes.Name);
  57. log.LogUser = roleClaim != null ? roleClaim.Value : "";
  58. }
  59. log.LogTitle = httpContext.Request.Path.HasValue ? httpContext.Request.Path.Value : "";
  60. log.LogDescribe = "request";
  61. log.LogType = 3;
  62. log.LogText = JsonConvert.SerializeObject(jobject);
  63. this.DbContext.SysLog.Add(log);
  64. this.DbContext.SaveChanges();
  65. return true;
  66. }
  67. catch
  68. {
  69. return false;
  70. }
  71. }
  72. }
  73. }