假设这样的场景:
[Route("api/test")]
public class TestController
{
private readonly ILogger<TestController> logger;
public TestController(ILogger<TestController> logger)
{
this.logger = logger;
}
[HttpPut]
public void Put(Guid id, [FromBody]FooModel model)
{
logger.LogInformation($"Putting {id}");
logger.LogTrace("Putting model {0}", Newtonsoft.Json.JsonConvert.SerializeObject(model));
try
{
// Omitted: actual PUT operation.
}
catch (Exception ex)
{
logger.LogError("Exception {0}", ex);
}
}
}
public class FooModel
{
string Bar { get; set; }
}
在这种情况下,LogInformation
来电会触发string.Format
来电,更糟糕的是,LogTrace
行会触发 SerializeObject
来电,无论LogLevel
。这似乎相当浪费。
Logging API中是否存在允许更懒惰的方法?我能想到的唯一解决方法是在模型上覆盖ToString
以创建非常详细的表示,并跳过使用JsonConvert.SerializeObject
作为工具。
答案 0 :(得分:5)
// put this function outside loop - if any
function set_click(el, value){
return el.click(function(){
var sure = confirm("text");
if (sure)
location.href ="privateServices/deleteZone.php?zone="+value;
}
}
// [...]
$('<td />', {text: data[i]}),
$('<td />').append(
function(){
return set_click($("<i [...] ></i>"), data[i]);
}
);
界面提供ILogger
方法:
IsEnabled
您将在GitHub上找到默认实现:https://github.com/aspnet/Extensions/blob/master/src/Logging/Logging/src/Logger.cs#L53