Tôi mới dùng NLog và phần lớn là thiết lập rất đơn giản.NLog tùy chỉnh LayoutRenderer - json: không thể làm việc
Vấn đề tôi đang gặp là thiết lập một LayoutRenderer tùy chỉnh
JsonLayoutRenderer.cs (namespace: NBT.Logging; dự án riêng biệt trong cùng một giải pháp)
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using Newtonsoft.Json.Linq;
using NLog;
using NLog.LayoutRenderers;
namespace NBT.Logging
{
[LayoutRenderer("json")]
public class JsonLayoutRenderer : LayoutRenderer
{
protected override void Append(StringBuilder builder, LogEventInfo logEvent)
{
dynamic logEntry = new JObject();
logEntry.TimeStamp = logEvent.TimeStamp.ToString("yyyy-MM-ddTHH:mm:ss.mmmzzz", CultureInfo.InvariantCulture);
logEntry.TimeStampUTC = logEvent.TimeStamp.ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ss.mmmZ", CultureInfo.InvariantCulture);
logEntry.Level = logEvent.Level.ToString();
logEntry.LoggerName = logEvent.LoggerName;
logEntry.Message = logEvent.FormattedMessage;
foreach(var prop in logEvent.Properties)
{
logEntry[prop.Key.ToString()] = prop.Value.ToString();
}
var json = logEntry.ToString(Formatting.None);
builder.Append(json);
}
}
}
Mã lấy từ https://gist.github.com/caevyn/9594522
NLog.config
<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" throwExceptions="true">
<extensions>
<add assembly="NLog.Targets.Redis"/>
<add assembly="NBT.Logging" />
</extensions>
<targets async="true">
<target xsi:type="Redis" name="redis" host="127.0.0.1" key="logstash" dataType="channel" layout="${longdate}|${level:uppercase=true}|${logger}|${message}|j|${json}"/>
</targets>
<rules>
<logger name="*" minlevel="Info" writeTo="redis" />
</rules>
</nlog>
Vì vậy, khai thác gỗ để redis hiển thị tất cả các mục bố trí bình thường nhưng không phải là $ {} json bit
"2014-05-17 12:36:58.7480|INFO|ExampleController|Index loaded|j|"
lẽ thiếu một cái gì đó đơn giản.
Cập nhật (Added đăng ký layoutRenderer để Global.asax.cs)
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
AuthConfig.RegisterAuth();
// Register custom Model Binders
ModelBinderConfig.RegisterModelBinders();
ConfigurationItemFactory.Default.LayoutRenderers.RegisterDefinition("json", typeof(JsonLayoutRenderer));
}
Bạn đã đăng ký trình kết xuất bố cục của mình với: 'ConfigurationItemFactory.Default.LayoutRenderers .RegisterDefinition (" json ", typeof (JsonLayoutRenderer));'? – nemesv
Tôi đã thấy điều này và ở giữa việc thực hiện nó vs đã đi tất cả kỳ lạ. Nó nên đi đâu? Tôi đang viết một ứng dụng web MVC. – mrdnk
Điều này sẽ được gọi là khởi động ứng dụng của bạn. Vì vậy, trong trường hợp web.applicaiton trong phương thức Global.asax.cs 'Application_Start'. – nemesv