using Dapper; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.DependencyInjection; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using TaskManager.Controllers; using TaskManager.Entity; using TaskManager.EntityFramework; namespace TaskManager.Controllers { public class LogController:ControllerBase { private readonly JobDbContext _dbContext; public LogController(JobDbContext dbContext) { _dbContext = dbContext; } public async Task> GetAll() { var log = await _dbContext.TaskLogs.ToListAsync(); return log; } [HttpGet("AddError")] public async Task AddError(string message,string taskname) { _dbContext.TaskLogs.Add(new TaskLog() { Info = message, Type = "错误",TaskName=taskname ,CreationTime=DateTime.Now}); var result =await _dbContext.SaveChangesAsync(); if (result > 0) { return true; } return false; } [HttpGet("AddInfo")] public async Task AddInfo(string message, string taskname) { _dbContext.TaskLogs.Add(new TaskLog() { Info = message, Type = "记录", TaskName = taskname, CreationTime = DateTime.Now }); var result = await _dbContext.SaveChangesAsync(); if (result > 0) { return true; } return false; } } //private readonly IServiceProvider _serviceProvider; //public LogController(IServiceProvider serviceProvider) //{ // _serviceProvider = serviceProvider; //} //public async Task> GetLogs() //{ // var dbcontext= _serviceProvider.GetRequiredService(); // var connection=dbcontext.Database.GetDbConnection(); // connection.Query("select top 10 * from logs"); //} }