我有一个类库,其中包装了一些自己的方法的NLog实现:
public static class MyLogger
{
private static readonly NLog.Logger Logger = NLog.LogManager.GetCurrentClassLogger();
public static void Log(EventType eventType, Exception exception, string message)
{
LogLevel level = LogLevelHelper.GetLogLevel(eventType);
Logger.Log(level, exception, message);
}
public static void LogInfo(string message)
{
Logger.Info(message);
}
public static void LogError(Exception exception, string message)
{
Logger.Error(exception, message);
}
}
然后我有一个Asp Net WebApp,其中包含对类库方法的一些调用:
[ApiController]
public class Runner
{
[HttpGet]
public void Foo()
{
MyLogger.Log(Logger.Enums.EventType.Error, new System.Exception(), "Log with eventype error");
MyLogger.LogInfo("Log info");
MyLogger.LogError(new System.Exception(), "Log error");
}
}
在那个项目( Runner )中,我得到了Nlog.config
所接受的MyLogger
:
<?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"
internalLogFile="${basedir}\logs\internal-nlog.txt">>
<targets>
<target name="logfile" xsi:type="File"
layout="${longdate} ${logger} ${message}${exception:format=ToString}"
fileName="${basedir}\logs\logfile.txt"
keepFileOpen="true"
encoding="utf-8" />
<target name="logconsole" xsi:type="Console" />
<target name="logdb"
xsi:type="Database"
connectionString="${appsettings:name=DatabaseConfiguration.ConnectionString}"
commandType="StoredProcedure"
commandText="[dbo].[NLog_AddEntry_p]"
>
<parameter name="@id" layout="${guid}" />
<parameter name="@user" layout="${aspnet-user-identity}" />
<parameter name="@createdOnUtc" layout="${date:universalTime=True}" />
<parameter name="@eventType" layout="${level}" />
<parameter name="@url" layout="${aspnet-request-url}" />
<parameter name="@machineName" layout="${machinename}" />
<parameter name="@applicationName" layout="${iis-site-name}" />
<parameter name="@serverClass" layout="${callsite}" />
<parameter name="@serverMessage" layout="${message}" />
<parameter name="@serverStack" layout="${exception:format=ToString,StackTrace}" />
<parameter name="@clientMessage" layout="${aspnet-request}" />
<parameter name="@clientStack" layout="" />
</target>
</targets>
<rules>
<logger name="*" minlevel="Info" writeTo="logconsole" />
<logger name="*" minlevel="Info" writeTo="logfile" />
<logger name="*" minlevel="Info" maxlevel="Fatal" writeTo="logdb" />
</rules>
</nlog>
但是,当然,日志信息来自MyLogger
,而不是Runner
。
所有aspnet-*
和iis-*
标签都是空的。
我如何从Asp Net项目中获取此信息?