use NLog.Web.AspNetCore package
nlog.config
internal-nlog.txt
using IServices.ITest;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using MODEL.MCommon;
using System;
using TestAPI.HelperTool;
using System.IO;
using System.Drawing;
using System.Threading.Tasks;
using OtpNet;
using Newtonsoft.Json.Linq;
using Microsoft.Extensions.Logging;
using Microsoft.AspNetCore.Authorization;
namespace TestAPI.Controllers.Test
{
/// <summary>
///
/// </summary>
[Route("api/[controller]/[action]")]
[ApiController]
public class ExampleController : ControllerBase
{
/// <summary>
///
/// </summary>
public IExampleService _exampleService;
/// <summary>
///
/// </summary>
public IConfiguration _configuration;
/// <summary>
///
/// </summary>
private readonly ILogger<ExampleController> _logger;
public ExampleController(IExampleService example, ILogger<ExampleController> logger, IConfiguration configuration)
{
_exampleService= example;
_configuration = configuration;
_logger = logger;
}
/// <summary>
/// Test
/// </summary>
/// <param name="a"></param>
/// <param name="b"></param>
/// <returns></returns>
[Authorize]
[HttpGet]
[EnableCors("any")]
public ReturnMessageModel FncT(string a, string b)
{
ReturnMessageModel returnmessagemodel = new ReturnMessageModel();
try
{
returnmessagemodel = _exampleService.FncT(a, b);
return returnmessagemodel;
}
catch (Exception ex)
{
_logger.LogError(ex.ToString());
returnmessagemodel.Status = "Fail";
returnmessagemodel.Message = "失敗";
returnmessagemodel.Data = null;
return returnmessagemodel;
}
}
}
}
Program.cs
/// <summary>
///
/// </summary>
/// <param name="args"></param>
public static void Main(string[] args)
{
var logger = NLog.Web.NLogBuilder.ConfigureNLog("nlog.config").GetCurrentClassLogger();
try
{
CreateWebHostBuilder(args).Build().Run();
}
catch (Exception ex)
{
//NLog: catch setup errors
logger.Error(ex, "Stopped program because of exception");
throw;
}
finally
{
// Ensure to flush and stop internal timers/threads before application-exit (Avoid segmentation fault on Linux)
NLog.LogManager.Shutdown();
}
}
appsetting.json
{
"Logging": {
"LogLevel": {
"Default": "Warning"
}
}
}
留言列表