码迷,mamicode.com
首页 > 其他好文 > 详细

msbuild 自定义日志输出

时间:2020-07-02 20:00:10      阅读:59      评论:0      收藏:0      [点我收藏+]

标签:bat   out   output   sage   store   config   info   png   打包   

自定义打包日志输出,过滤不想看到的无关日志。

原理

[visual studio 2013 - How to have MSBuild quiet output but with error/warning summary - Stack Overflow]
(https://stackoverflow.com/questions/25565610/how-to-have-msbuild-quiet-output-but-with-error-warning-summary )

[IEventSource Interface (Microsoft.Build.Framework) | Microsoft Docs]
(https://docs.microsoft.com/en-us/dotnet/api/microsoft.build.framework.ieventsource?view=msbuild-16-netcore )

代码

using System;
using Microsoft.Build.Utilities;
using Microsoft.Build.Framework;

public class CustomLogger : Logger
{
    private int warnings = 0;
    private int errors = 0;

    public override void Initialize(IEventSource eventSource)
    {
        eventSource.WarningRaised += (s, e) => ++warnings;
        eventSource.ErrorRaised += (s, e) =>
        {
            ++errors;
            string line = String.Format("ERROR {0}({1},{2}): ", e.File, e.LineNumber, e.ColumnNumber);
            Console.WriteLine(line + e.Message);
        };

        eventSource.BuildFinished += (s, e) =>
        {
            Console.WriteLine(errors == 0 ? "Build succeeded." : "Build failed.");
            Console.WriteLine(String.Format("    {0} Warning(s)", warnings));
            Console.WriteLine(String.Format("    {0} Error(s)", errors));
        };
    }
}

命令

编译自定义日志组件 DLL 的命令:

csc /t:library CustomLogger.cs /reference:Microsoft.Build.Utilities.v4.0.dll;Microsoft.Build.Framework.dll

编译命令(日志配置):
msbuild /logger:CustomLogger.dll /noconsolelogger

编译命令(日志配置),指定 CustomLogger.dll 目录:
msbuild /m /p:Configuration=Release /logger:".\Build\tools\CustomLogger.dll" /noconsolelogger

效果

msbuild 编译安静了很多,没有错误的话,就没有任何输出。

技术图片

当然,如果只是想仅输出错误,有更简单的方法:

msbuild -m -p:Configuration=Release /consoleloggerparameters:ErrorsOnly

使用 /consoleloggerparameters:ErrorsOnly 即可。

P.S.
nuget 命令,可使用 -v q 参数简化输出。
[NuGet CLI restore command | Microsoft Docs]
(https://docs.microsoft.com/en-us/nuget/reference/cli-reference/cli-ref-restore )

原文链接:
https://www.cnblogs.com/jasongrass/p/13226498.html

msbuild 自定义日志输出

标签:bat   out   output   sage   store   config   info   png   打包   

原文地址:https://www.cnblogs.com/jasongrass/p/13226498.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!