166 lines
5.4 KiB
C#
166 lines
5.4 KiB
C#
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System;
|
|
using WebAPI.Services.Interfaces;
|
|
using System.Diagnostics;
|
|
using Church.Net.Entity;
|
|
using Church.Net.Entity.Interface;
|
|
using Church.Net.DAL.EFCoreDBF;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Newtonsoft.Json;
|
|
|
|
namespace WebAPI.Services
|
|
{
|
|
public class DbLoggingService : ILoggingService
|
|
{
|
|
private readonly ICrudDAL<LogInfo> crudDAL;
|
|
|
|
public DbLoggingService(ICrudDAL<LogInfo> crudDAL)
|
|
{
|
|
this.crudDAL = crudDAL;
|
|
}
|
|
|
|
public int Error(Exception exception)
|
|
{
|
|
string msg = exception.Message.ToString();
|
|
string detailMsg = exception.InnerException != null ? exception.InnerException.Message : "";
|
|
|
|
LogInfo log = new LogInfo(LogLevel.Error)
|
|
{
|
|
Message = msg,
|
|
DetailMessage = detailMsg,
|
|
Source = exception.Source,
|
|
StackTrace = exception.StackTrace,
|
|
UserId = ""
|
|
};
|
|
|
|
return AppendLog(log);
|
|
}
|
|
|
|
public int Error(Exception exception, string from)
|
|
{
|
|
string msg = exception.Message.ToString();
|
|
string detailMsg = exception.InnerException != null ? exception.InnerException.Message : "";
|
|
|
|
LogInfo log = new LogInfo(LogLevel.Error)
|
|
{
|
|
Url = from,
|
|
Message = msg,
|
|
DetailMessage = detailMsg,
|
|
Source = exception.Source,
|
|
StackTrace = exception.StackTrace
|
|
};
|
|
return AppendLog(log);
|
|
}
|
|
|
|
public int Error(Exception exception, string from, string detailMsg)
|
|
{
|
|
string msg = exception.Message.ToString();
|
|
|
|
string exceptionDetailMsg = exception.InnerException != null ? exception.InnerException.Message : "";
|
|
if (false == string.IsNullOrEmpty(exceptionDetailMsg))
|
|
{
|
|
detailMsg += Environment.NewLine + "=========== Exception Detail ==========";
|
|
detailMsg += Environment.NewLine + exceptionDetailMsg;
|
|
|
|
}
|
|
LogInfo log = new LogInfo(LogLevel.Error)
|
|
{
|
|
Url = from,
|
|
Message = msg,
|
|
DetailMessage = detailMsg,
|
|
Source = exception.Source,
|
|
StackTrace = exception.StackTrace
|
|
};
|
|
return AppendLog(log);
|
|
}
|
|
|
|
public IEnumerable<LogInfo> GetAllErrors(DateTime? rangeStart = null, DateTime? rangeEnd = null)
|
|
{
|
|
IQueryable<LogInfo> query = crudDAL.GetDbSet().AsQueryable();
|
|
|
|
if (rangeStart != null)
|
|
{
|
|
query = query.Where(l => l.Time >= rangeStart);
|
|
}
|
|
|
|
if (rangeEnd != null)
|
|
{
|
|
query = query.Where(l => l.Time <= rangeEnd);
|
|
}
|
|
|
|
return query.OrderByDescending(l => l.Time).ToList();
|
|
}
|
|
|
|
private int AppendLog(LogInfo log)
|
|
{
|
|
try
|
|
{
|
|
crudDAL.Create(log);
|
|
return log.TrackNo;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
WriteWindowsEventLog(ex);
|
|
WriteWindowsEventLog(log);
|
|
return -1;
|
|
}
|
|
}
|
|
private void WriteWindowsEventLog(LogInfo log)
|
|
{
|
|
//var fileLogService = new FileLoggingService();
|
|
//fileLogService.Error(new Exception(
|
|
// log.Source + Environment.NewLine + Environment.NewLine +
|
|
// log.Message + Environment.NewLine + Environment.NewLine +
|
|
// log.DetailMessage
|
|
// ));
|
|
EventLog.WriteEntry("BeyondAPI",
|
|
log.Source + Environment.NewLine + Environment.NewLine +
|
|
log.Message + Environment.NewLine + Environment.NewLine +
|
|
log.DetailMessage
|
|
, EventLogEntryType.Error);
|
|
}
|
|
private void WriteWindowsEventLog(Exception ex)
|
|
{
|
|
//var fileLogService = new FileLoggingService();
|
|
//fileLogService.Error(ex, "Can Not Append Error Log");
|
|
EventLog.WriteEntry("BeyondAPI", $"Can Not Append Error Log:{ex.Message}", EventLogEntryType.Error);
|
|
}
|
|
|
|
public void Log(string message, object detail)
|
|
{
|
|
LogInfo log = new LogInfo(LogLevel.Info)
|
|
{
|
|
Message = message,
|
|
DetailMessage = JsonConvert.SerializeObject(detail,Formatting.Indented) ,
|
|
//Source = exception.Source,
|
|
//StackTrace = exception.StackTrace
|
|
};
|
|
AppendLog(log);
|
|
}
|
|
|
|
public void Warning(string message, object detail)
|
|
{
|
|
|
|
LogInfo log = new LogInfo(LogLevel.Warning)
|
|
{
|
|
Message = message,
|
|
DetailMessage = JsonConvert.SerializeObject(detail, Formatting.Indented),
|
|
//Source = exception.Source,
|
|
//StackTrace = exception.StackTrace
|
|
};
|
|
AppendLog(log);
|
|
}
|
|
|
|
IEnumerable<LogInfo> ILoggingService.GetAllErrors(DateTime? rangeStart, DateTime? rangeEnd)
|
|
{
|
|
throw new NotImplementedException();
|
|
}
|
|
|
|
public IEnumerable<LogInfo> GetAllLogs(LogLevel logLevel, DateTime? rangeStart = null, DateTime? rangeEnd = null)
|
|
{
|
|
throw new NotImplementedException();
|
|
}
|
|
}
|
|
}
|