You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

152 lines
4.4 KiB

using Magicodes.ExporterAndImporter.Excel;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using OfficeOpenXml.FormulaParsing.Excel.Functions.Text;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using TaskManager.Controllers;
using TaskManager.Entity;
using TaskManager.EntityFramework;
using TaskManager.EntityFramework.Repository;
using Wood.Service.Controllers;
using System;
using System.Threading;
using System.Threading.Channels;
using System.Threading.Tasks;
namespace TaskManager.Controllers
{
public class LogController
{
private readonly Channel<TaskLog> _logChannel;
public LogController(int bufferSize = 1000)
{
// 创建有界通道,设置缓冲区大小
var options = new BoundedChannelOptions(bufferSize)
{
FullMode = BoundedChannelFullMode.Wait
};
_logChannel = Channel.CreateBounded<TaskLog>(options);
}
public void EnqueueLog(TaskLog log)
{
_logChannel.Writer.TryWrite(log);
}
//private readonly LogBackgroundService _logService;
//public LogController(JobDbContext context, IServiceProvider builder, IConfiguration configuration, IRepository<TaskLog> repository, LogBackgroundService logService) : base(context, builder, configuration, repository)
//{
// _logService = logService;
//}
//[HttpGet]
//public async Task<List<TaskLog>> GetAll()
//{
// var log = await _context.TaskLogs.ToListAsync();
// return log;
//}
public ChannelReader<TaskLog> GetLogReader()
{
return _logChannel.Reader;
}
[NonAction]
//[HttpGet("AddError")]
public async Task<bool> AddError(string message, string taskname, Guid taskid, string version)
{
var log = new TaskLog
{
Info = message,
Type = "错误",
TaskName = taskname,
CreationTime = DateTime.Now,
TaskId = taskid,
Version = version?.ToString()
};
EnqueueLog(log);
//_context.TaskLogs.Add(log);
//var result = await _context.SaveChangesAsync();
return true; // 日志已入队,视为成功
}
[NonAction]
//[HttpGet("AddInfo")]
public async Task<bool> AddInfo(string message, string taskname, Guid taskid, string version)
{
var log = new TaskLog
{
Info = message,
Type = "信息",
TaskName = taskname,
CreationTime = DateTime.Now,
TaskId = taskid,
Version = version?.ToString()
};
EnqueueLog(log);
//_context.TaskLogs.Add(log);
//var result = await _context.SaveChangesAsync();
return true;
}
[NonAction]
//[HttpGet("AddPostRequest")]
public async Task<bool> AddPostRequest(string message, string taskname, Guid taskid, string version, string remark)
{
var log = new TaskLog
{
Info = message,
Type = "请求",
TaskName = taskname,
CreationTime = DateTime.Now,
TaskId = taskid,
Version = version?.ToString(),
Remark = remark
};
EnqueueLog(log);
//_context.TaskLogs.Add(log);
//var result = await _context.SaveChangesAsync();
return true;
}
[NonAction]
//[HttpGet("AddPostResponse")]
public async Task<bool> AddPostResponse(string message, string taskname, Guid taskid, string version, string remaek)
{
var log = new TaskLog
{
Info = message,
Type = "应答",
TaskName = taskname,
CreationTime = DateTime.Now,
TaskId = taskid,
Version = version?.ToString(),
Remark = remaek
};
EnqueueLog(log);
return true;
}
}
}