自己設定 Nlog.config 所存在的資料夾。
string
assemblyFolder = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
NLog.LogManager.Configuration = new NLog.Config.XmlLoggingConfiguration(assemblyFolder
+ "\\config\\Nlog.config", true);
假設不要有config ,直接輸入xml 字串 代替config 。( xsi:type 要改成type)
string xml =
string xml =
@"<nlog>
<targets>
<target name='f' type='File' fileName='${basedir}/logs/${shortdate}.log'
layout='${message}' />
</targets>
<rules>
<logger name='*' minlevel='Trace'
writeTo='f' />
</rules>
</nlog>";
StringReader
sr2 = new StringReader(xml);
XmlReader
xr2 = XmlReader.Create(sr2);
XmlLoggingConfiguration
config2 = new XmlLoggingConfiguration(xr2,
null);
LogManager.Configuration
= config2;
logger.Trace("test");
沒有留言:
張貼留言