当我使用nlog进行错误和记录时,我遇到了问题。这是我的代码:
错误:设置属性'布局'时出错在文件目标[文件]
Nlog.config
<target name="file" xsi:type="File" layout="${date}|${level}|${logger}|${steps_guid}|${machinename}|${windows-identity:domain=false}--${message} ${exception:format=message,stacktrace:separator=*" fileName="E:\myapplication.log" />
<target xsi:type="Database" name="database" connectionString="Data Source=xxx;Initial Catalog=xxx;Integrated Security=True" commandText="exec [dbo].[LOGINNG_CREATE] @steps_guid,@log_application ,@log_date,@log_level ,@log_logger , @log_message ,@log_machine_name , @log_user_name ,@log_call_site ,@log_thread ,@log_exception , @log_stacktrace ">
<parameter name="@steps_guid" layout="${steps_guid}"/>
<parameter name="@log_application" layout="${application}"/>
<parameter name="@log_date" layout="${date}"/>
<parameter name="@log_level" layout="${level}"/>
<parameter name="@log_logger" layout="${logger}"/>
<parameter name="@log_message" layout="${message}"/>
<parameter name="@log_machine_name" layout="${machinename}"/>
<parameter name="@log_user_name" layout="${windows-identity:domain=true}"/>
<parameter name="@log_call_site" layout="${callsite:filename=true}"/>
<parameter name="@log_thread" layout="${threadid}"/>
<parameter name="@log_exception" layout="${exception}"/>
<parameter name="@log_stacktrace" layout="${stacktrace}"/>
</target>
和c#代码,我不知道是否有必要:
Logger loggerClass = LogManager.GetCurrentClassLogger();
GlobalDiagnosticsContext.Set("steps_guid", steps_guid);
return loggerClass;
我这样使用它:
public bool Login(Entity.USERS user)
{
try
{
this.PageRepository = new xxx.Business.UserBusiness();
logger.Info("User service's instance created");
return this.PageRepository.Login(user);
}
catch (Exception ex)
{
logger.ErrorException("User services login Error", ex);
return false;
}
}