我想同时将来自 Nlog 的信息发送到一个文件和一个 web 服务。该文件工作正常。Web 服务不会在控制器处遇到断点。
我尝试了这里推荐的步骤https://github.com/NLog/NLog/wiki/WebService-target,这里https://github.com/NLog/NLog/issues/1996并理解它应该是可行的,因为https:/ /github.com/NLog/NLog/pull/1912并使用以下代码检测记录器:
测试工作正常:
DebugTarget target = new DebugTarget();
target.Layout = "${message}";
NLog.Config.SimpleConfigurator.ConfigureForTargetLogging(target, LogLevel.Debug);
Logger logger = LogManager.GetLogger("ws");
logger.Debug("log message"); <--- here I expected to hit the breakpoint after this step
logger.Debug("another log message");
我的 Nlog.config
<targets>
<target xsi:type='WebService'
name= 'ws'
protocol= 'HttpPost'
url= 'http://localhost:8082/API/Nlog'
encoding= 'UTF-8'
preAuthenticate= 'true'
proxyType="NoProxy">
<parameter name="sourceSite" type='System.String' layout="${aspnet-request:serverVariable=Url:format=ToString}" />
<parameter name="sourceApplication" type='System.String' layout="${iis-site-name:format=ToString}" />
<parameter name="message" type='System.String' layout="${message:format=ToString}" />
<parameter name="innerException" type='System.String' layout="${exception:format=string,message,method:maxInnerExceptionLevel=5:innerFormat=shortType,message,method}}" />
<parameter name="level" type='System.String' layout="${level:uppercase=true}" />
<parameter name="stackTrace" type='System.String' layout="${exception:format=ToString,StackTrace}${newline}" />
<parameter name='windowsUserName' type='System.String' layout='${windows-identity:userName=true:domain=true:format=ToString}' />
<header name='WindowsUserName' layout='${windows-identity:userName=true:domain=true}' />
<header name='xapikey' layout='keyValue' />
</target>
<target name="file" xsi:type="File" fileName="${basedir}/log.txt" archiveAboveSize="52428800" layout="${longdate}|${level:uppercase=true}|${logger}|${message}|${exception:format=ToString,StackTrace}${newline}"/>
</targets>
<rules>
<logger name="*" minlevel="Off" writeTo="file"/>
<logger name="ws" minlevel="Trace" writeTo="ws"/>
</rules>
控制器调用,但断点不会被击中:
public class NlogController : BaseController (which has BaseController : ApiController)
{
protected new static readonly Logger Logger = LogManager.GetCurrentClassLogger();
/// <summary>
///
/// </summary>
/// <param name="data"></param>
public void Post([FromBody] NlogData data)
{ //Breakpoint placed here
Manager.StoreLoggingInformation(data);
}
}
NLogData 定义
public class NlogData
{
public string SourceSite { get; set; }
public string SourceApplication { get; set; }
public string Message { get; set; }
public string InnerException { get; set; }
public string Level { get; set; }
public string StackTrace { get; set; }
public string WindowsUserName { get; set; }
}
我希望在我的 NlogController 中达到断点并查看“数据”中的内容。