Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
272 views
in Technique[技术] by (71.8m points)

xml - How to change the value of nlog in app.config file using powershell

I am really struggle to get this working. I can update the app settings using powershell but there is 1 line which i need to modify in Target node. Here is a portion of my xml

  <startup>
  <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.1" />
 </startup>
 <entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory,  
 EntityFramework" />
<providers>
  <provider invariantName="System.Data.SqlClient"   
  type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
  </entityFramework>
  <nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"    
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" throwExceptions="true">
  <targets>
  <target name="f1" xsi:type="File" fileName="test.log.txt" layout="${threadname} 
  ${longdate} ${callsite} ${logger}[${level}] ${message}" />
  <target name="console" xsi:type="Console" layout="${threadname} ${longdate} ${callsite}   
  ${logger}[${level}] ${message}" />
   </targets>
  <rules>
  <!--<logger name="Quartz.*" level="Warn" />-->
  <logger name="*" writeTo="f1" minlevel="Info" />
  <logger name="*" writeTo="console" minlevel="Debug" />
  </rules>
  </nlog>

In the target node where it says test.log.txt I just want to have a file path with this like "c:projectlogs est.log.txt"

This is what i used for app settings and it worked

$webConfig = 'C:Projectsapp.exe.config'
$webConfigXml = [xml](gc $webConfig

$appSetting = $webConfigXml.configuration.appSettings.add | where {$_.Key -eq 'userName'}
$appSetting.value = 'testUser'

Struggling alot with this one

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You could do the following:

$xml = [xml](gc $webConfig)
$t = $xml.getElementsByTagName("targets")
$t.target | % { if ($_.fileName -eq 'test.log.txt') {$_.setAttribute('fileName','c:projectlogs	est.log.txt')} }
$xml.outerXml | Out-File $webConfig

Slightly different way of selecting the nodes you require but should do what you want.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...