使用NLog与AspNet布局渲染器在AspNet vNext (MVC 6)

时间:2022-07-12 11:59:51

I am trying to integrate NLog in AspNet 5 (or using the new name AspNet Core 1.0) web app. Not sure if it is possible at all but I want to log the currently logged in user. This is my NLog config file.

我正在尝试将NLog集成到AspNet 5(或使用新名称AspNet Core 1.0) web应用程序中。我不确定是否可能,但我想记录当前登录的用户。这是我的NLog配置文件。

<?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"
      autoReload="true">
  <targets>
    <target name="logfile"
            xsi:type="File"
            fileName="file.txt"
            layout="${longdate}|${message}|${identity}|${aspnet-user-identity}" />
  </targets>
  <rules>
    <logger name="WebApplication2.*" minlevel="Info" writeTo="logfile" />
  </rules>
</nlog>

The thing is that I get

我得到的是

System.ArgumentException: LayoutRenderer cannot be found: 'aspnet-user-identity'

系统。无法找到LayoutRenderer:‘aspnet用户身份’

My project.json file looks like:

我的项目。json文件看起来像:

  "dependencies": {
    .....
    "NLog.Framework.logging": "1.0.0-rc1-final",
    "NLog": "4.4.0-alpha1",
    "NLog.Config": "4.3.0-beta1",
    "NLog.Extended": "4.0.0-rc",
    "NLog.Web": "4.1.0"
  },

Maybe I am missing something, or it is not yet supported by NLog ?

也许我漏掉了什么,或者NLog还没有支持它?

EDIT: I found out that auto load of extensions is not currently supported. So, I have to modify my NLog.config like:

编辑:我发现目前不支持自动加载扩展。我需要修改NLog。配置:

 <extensions>
    <add assembly="NLog.Web"/>
    <add assembly="NLog.Extended"/>
  </extensions>

But now I get blank input for the current user. Any ideas ?

但是现在我得到了当前用户的空白输入。什么好主意吗?

1 个解决方案

#1


2  

The internal log will give some more info why loading the extension failed.

内部日志将提供关于为什么加载扩展失败的更多信息。

There are also other ways to load the extensions, the assemblyFile attribute and programmatically:

还有其他方法可以加载扩展名、程序集文件属性并以编程方式加载:

//target
ConfigurationItemFactory.Default.Targets
                        .RegisterDefinition("MyFirst", typeof(MyNamespace.MyFirstTarget));

//layout renderer
ConfigurationItemFactory.Default.LayoutRenderers
                        .RegisterDefinition("hello-world", typeof(MyNamespace.HelloWorldLayoutRenderer ));

Edit: did some tests. This works in loading the assembly:

编辑:做了一些测试。这适用于加载程序集:

<extensions> <add assembly="NLog.Web" /> </extensions>

<扩展> <添加组装= " nlog。web> < /扩展>

But too bad NLog.Web isn't ASP.NET 5 compatible, yet. We can't use HttpContext.Current there.

但可惜NLog。网络不是ASP。净5兼容。我们不能用HttpContext。当前。

edit: an ASP.NET 5 compatible version of NLog.Web is now available! See NuGet

编辑:一个ASP。NET 5兼容版本的NLog。网络是现在可用!看到NuGet

#1


2  

The internal log will give some more info why loading the extension failed.

内部日志将提供关于为什么加载扩展失败的更多信息。

There are also other ways to load the extensions, the assemblyFile attribute and programmatically:

还有其他方法可以加载扩展名、程序集文件属性并以编程方式加载:

//target
ConfigurationItemFactory.Default.Targets
                        .RegisterDefinition("MyFirst", typeof(MyNamespace.MyFirstTarget));

//layout renderer
ConfigurationItemFactory.Default.LayoutRenderers
                        .RegisterDefinition("hello-world", typeof(MyNamespace.HelloWorldLayoutRenderer ));

Edit: did some tests. This works in loading the assembly:

编辑:做了一些测试。这适用于加载程序集:

<extensions> <add assembly="NLog.Web" /> </extensions>

<扩展> <添加组装= " nlog。web> < /扩展>

But too bad NLog.Web isn't ASP.NET 5 compatible, yet. We can't use HttpContext.Current there.

但可惜NLog。网络不是ASP。净5兼容。我们不能用HttpContext。当前。

edit: an ASP.NET 5 compatible version of NLog.Web is now available! See NuGet

编辑:一个ASP。NET 5兼容版本的NLog。网络是现在可用!看到NuGet