开发者

Get the current logging verbosity level in my MSBuild script

Our MSBuild scripts use the Exec task 开发者_JS百科to call a few command line applications. Most of these applications have their own output verbosity settings which I would like to have be the same as the verbosity level of the MSBuild script calling them.

Is there a way for me to get the logging verbosity level of my MSBuild process?

I thought I could write a custom task to handle this, but poking around the MSBuild API, I couldn't find any properties or classes that would give me the verbosity level.


Shortly after asking my questions, I noticed that MSBuild 4 exposes System.Environment.CommandLine as a property function, which should include any verbosity arguments. With the following bit of parsing, you can create several boolean properties that will tell you the current logging level:

<PropertyGroup>
  <CommandLine>$([System.Environment]::CommandLine.Trim().ToLower())</CommandLine>
  <IsQuietVerbosity>False</IsQuietVerbosity>
  <IsMinimalVerbosity>False</IsMinimalVerbosity>
  <IsNormalVerbosity>True</IsNormalVerbosity>
  <IsDetailedVerbosity>False</IsDetailedVerbosity>
  <IsDiagnosticVerbosity>False</IsDiagnosticVerbosity>
 </PropertyGroup>

 <PropertyGroup Condition="'$(CommandLine.Contains(&quot;/v&quot;))' == 'True'">
  <IndexOfLastVerbosityArg>$(CommandLine.LastIndexOf("/v"))</IndexOfLastVerbosityArg>
  <IndexOfVerbosityArg>$(CommandLine.IndexOf(":", $(IndexOfLastVerbosityArg)))</IndexOfVerbosityArg>
  <IndexOfVerbosityArg>$([MSBuild]::Add($(IndexOfVerbosityArg), 1))</IndexOfVerbosityArg>
  <IndexOfEndOfVerbosityArg>$(CommandLine.IndexOf(" ", $(IndexOfVerbosityArg)))</IndexOfEndOfVerbosityArg>
  <IndexOfEndOfVerbosityArg Condition="'$(IndexOfEndOfVerbosityArg)' == '-1'">$(CommandLine.Length)</IndexOfEndOfVerbosityArg>
  <LengthOfVerbosityArg>$([MSBuild]::Subtract($(IndexOfEndOfVerbosityArg), $(IndexOfVerbosityArg)))</LengthOfVerbosityArg>
  <VerbosityLevel>$(CommandLine.Substring($(IndexOfVerbosityArg), $(LengthOfVerbosityArg)).Trim())</VerbosityLevel>
  <IsQuietVerbosity>$(VerbosityLevel.StartsWith('q'))</IsQuietVerbosity>
  <IsMinimalVerbosity>$(VerbosityLevel.StartsWith('m'))</IsMinimalVerbosity>
  <IsNormalVerbosity>$(VerbosityLevel.StartsWith('n'))</IsNormalVerbosity>
  <IsDiagnosticVerbosity>$(VerbosityLevel.StartsWith('di'))</IsDiagnosticVerbosity>
  <IsDetailedVerbosity Condition="'$(IsDiagnosticVerbosity)' == 'False'">$(VerbosityLevel.StartsWith('d'))</IsDetailedVerbosity>
 </PropertyGroup>

Remember, this will only work in MSBuild 4+.

Ugly? Yup. Kludgy? Maybe. Does it work. Yup!


You can't : http://social.msdn.microsoft.com/Forums/en-US/msbuild/thread/510f07b4-c5f7-43a8-b7cb-e3c398841725/

Instead, you can set a property of yours (passing it with the command line for example) which contains the verbosity level.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜