0

我们在 ASP .NET MVC Web 应用程序 .NET Framework 4.7.2 中使用 NLog。应用程序使用 Microsoft 身份平台进行用户身份验证。

我们使用 ${identity} 将 NLog 映射到数据库表(Web.config 的一部分在文中)。

但是 ${identity} 包含 auth:Cookies: 而不是真实的用户名。

如何将其修复为真实用户名?

<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<targets>
    <target name="database" xsi:type="Database">
        <connectionStringName>QtContext</connectionStringName>
        <commandText>
            insert into dbo.NLOG (
            Application, Logged, Level, Message, Username, ServerName, Port, Url, Https, ServerAddress, RemoteAddress, Logger, CallSite, Exception
            ) values (
            @Application, @Logged, @Level, @Message, @Username, @ServerName, @Port, @Url, @Https, @ServerAddress, @RemoteAddress, @Logger, @Callsite, @Exception
            );
        </commandText>
        <parameter name="@application" layout="${appsetting:name=AppName:default=Unknown\: set AppName in appSettings}" />
        <parameter name="@logged" layout="${date:format=yyyy-MM-ddTHH\:mm\:ss.fff}" />
        <parameter name="@level" layout="${level}" />
        <parameter name="@message" layout="${message}" />
        <parameter name="@username" layout="${identity}" />
        <parameter name="@serverName" layout="${aspnet-request:serverVariable=SERVER_NAME}" />
        <parameter name="@port" layout="${aspnet-request:serverVariable=SERVER_PORT}" />
        <parameter name="@url" layout="${aspnet-request:serverVariable=HTTP_URL}" />
        <parameter name="@https" layout="${when:inner=1:when='${aspnet-request:serverVariable=HTTPS}' == 'on'}${when:inner=0:when='${aspnet-request:serverVariable=HTTPS}' != 'on'}" />
        <parameter name="@serverAddress" layout="${aspnet-request:serverVariable=LOCAL_ADDR}" />
        <parameter name="@remoteAddress" layout="${aspnet-request:serverVariable=REMOTE_ADDR}:${aspnet-request:serverVariable=REMOTE_PORT}" />
        <parameter name="@logger" layout="${logger}" />
        <parameter name="@callSite" layout="${callsite}" />
        <parameter name="@exception" layout="${exception:tostring}" />
    </target>
</targets>
<rules>
    <logger name="*" writeTo="database" />
</rules>
4

0 回答 0