2014-05-17 23 views
6

Sono nuovo in NLog e per la maggior parte è molto semplice da configurare.Layout layout personalizzato NLog - json: impossibile lavorare

Il problema che sto avendo è la creazione di un LayoutRenderer personalizzato

JsonLayoutRenderer.cs (namespace: NBT.Logging; progetto separato all'interno della stessa soluzione)

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); 

     } 
    } 
} 

codice preso dal 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> 

Quindi la registrazione a Redis mostra tutti gli elementi di layout normali, ma non il $ {} JSON il bit

"2014-05-17 12:36:58.7480|INFO|ExampleController|Index loaded|j|"

probabilmente manca qualcosa di semplice.

Update (Informazione Registrare layoutRenderer a 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)); 

    } 
+0

Hai registrato il renderer di layout con: 'ConfigurationItemFactory.Default.LayoutRenderers .RegisterDefinition (" json ", typeof (JsonLayoutRenderer));'? – nemesv

+0

Ho visto questo e nel mezzo di implementarlo contro è andato tutto strano. Dove dovrebbe andare? Sto scrivendo un'app Web MVC. – mrdnk

+0

Questo dovrebbe essere chiamato l'avvio della tua app. Quindi nel caso di una web.applicaiton nel metodo Global.asax.cs 'Application_Start'. – nemesv

risposta

1

E 'anche possibile aggiungere un assembly che contiene il renderer NLog.config layout personalizzato:

<extensions> 
 
    <add assembly="MyNLogExtensions"/> 
 
</extensions>

Vedere this other post.

Problemi correlati