103 lines
4.4 KiB
C#
103 lines
4.4 KiB
C#
using AutoMapper;
|
|
using IRaCIS.Application.ExpressionExtend;
|
|
using IRaCIS.Application.Interfaces;
|
|
using IRaCIS.Application.ViewModels;
|
|
using IRaCIS.Core.Application.Contracts.RequestAndResponse;
|
|
using IRaCIS.Core.Domain.Interfaces;
|
|
using IRaCIS.Core.Domain.Models;
|
|
using IRaCIS.Core.Infrastructure;
|
|
using IRaCIS.Infra.Data.ExpressionExtend;
|
|
using Microsoft.AspNetCore.Http;
|
|
using System;
|
|
using System.Linq;
|
|
using System.Linq.Expressions;
|
|
|
|
namespace IRaCIS.Application.Services
|
|
{
|
|
public class LogService : ILogService
|
|
{
|
|
private readonly ISystemLogRepository _systemLogRepository;
|
|
private readonly IUserRepository _userRepository;
|
|
private readonly IMapper _mapper;
|
|
private readonly IHttpContextAccessor _context;
|
|
|
|
public LogService(ISystemLogRepository systemLogRepository, IUserRepository userRepository, IMapper mapper, IHttpContextAccessor context)
|
|
{
|
|
_systemLogRepository = systemLogRepository;
|
|
_userRepository = userRepository;
|
|
_mapper = mapper;
|
|
_context = context;
|
|
}
|
|
public PageOutput<SystemLogDTO> GetLogList(QueryLogQueryDTO param)
|
|
{
|
|
Expression<Func<SystemLog, bool>> logLambda = x => true;
|
|
DateTime bTime = new DateTime(param.BeginTime.Year, param.BeginTime.Month, param.BeginTime.Day, 0, 0, 1);
|
|
DateTime eTime = new DateTime(param.EndTime.Year, param.EndTime.Month, param.EndTime.Day, 23, 59, 59);
|
|
logLambda = logLambda.And(o => o.RequestTime >= bTime && o.RequestTime <= eTime);
|
|
if (!string.IsNullOrWhiteSpace(param.LogCategory))
|
|
{
|
|
logLambda = logLambda.And(o => o.LogCategory == param.LogCategory);
|
|
}
|
|
if (!string.IsNullOrWhiteSpace(param.Keyword.Trim()))
|
|
{
|
|
logLambda = logLambda.And(o => o.Params.Contains(param.Keyword.Trim()) || o.Result.Contains(param.Keyword.Trim()));
|
|
}
|
|
var a = _systemLogRepository.Find(logLambda).ToList();
|
|
var logQueryable = from log in _systemLogRepository.GetAll().Where(logLambda)
|
|
select new SystemLogDTO
|
|
{
|
|
Id = log.Id,
|
|
LogCategory = log.LogCategory,
|
|
ApiPath = log.ApiPath,
|
|
Params = log.Params,
|
|
Result = log.Result,
|
|
ElapsedMilliseconds = log.ElapsedMilliseconds,
|
|
RequestTime = log.RequestTime,
|
|
ClientIP = log.ClientIP,
|
|
Message = log.Message,
|
|
Status = log.Status,
|
|
OptUserId = log.OptUserId,
|
|
OptUserName = log.OptUserName
|
|
};
|
|
var count = logQueryable.Count();
|
|
var propName = string.Empty;
|
|
if (string.IsNullOrWhiteSpace(param.SortField))
|
|
{
|
|
propName = "RequestTime";
|
|
param.Asc = false;
|
|
}
|
|
else
|
|
{
|
|
propName = param.SortField;
|
|
}
|
|
logQueryable = param.Asc
|
|
? logQueryable.OrderBy(propName)
|
|
: logQueryable.OrderByDescending(propName);
|
|
logQueryable = logQueryable
|
|
.Skip((param.PageIndex - 1) * param.PageSize)
|
|
.Take(param.PageSize);
|
|
var logList = logQueryable.ToList();
|
|
|
|
return new PageOutput<SystemLogDTO>(param.PageIndex, param.PageSize, count, logList);
|
|
}
|
|
|
|
public IResponseOutput SaveLog2Db(SystemLogDTO input)
|
|
{
|
|
//string ua = _context.HttpContext.Request.Headers["User-Agent"];
|
|
//var client = UAParser.Parser.GetDefault().Parse(ua);
|
|
//var device = client.Device.Family;
|
|
//device = device.ToLower() == "other" ? "" : device;
|
|
//input.Browser = client.UA.Family;
|
|
//input.Os = client.OS.Family;
|
|
//input.Device = device;
|
|
//input.BrowserInfo = ua;
|
|
|
|
input.ClientIP = IPHelper.GetIP(_context?.HttpContext?.Request);
|
|
|
|
_systemLogRepository.Add(_mapper.Map<SystemLog>(input));
|
|
var success = _systemLogRepository.SaveChanges();
|
|
return ResponseOutput.Result(success);
|
|
}
|
|
}
|
|
}
|