使用“StackExchange.Precompilation”,如何输出构建错误?

Using `StackExchange.Precompilation`, how can I output build errors?

我正在使用 StackExchange.Precompilation 在构建时执行一些 C# 语法转换。我希望能够在构建期间从我的 ICompileModule 中抛出异常,并将这些输出到 Visual Studio 的输出 window.

我创建了一个自定义异常类型 PrecompilationException 来表示这些语法转换期间发生的错误。这是我的例外 class:

[Serializable]
public class PrecompilationException : Exception {

    public PrecompilationException() : base() { }

    public PrecompilationException(string message) : base(message) { }

    public PrecompilationException(string message, Exception innerException) : base(message, innerException) { }

    protected PrecompilationException(SerializationInfo info, StreamingContext context) : base(info, context) { }
}

当抛出其中一个异常时,输出window显示一个SerializationException,其消息是"Type is not resolved for member 'PrecompilationException...",但是没有PrecompilationException 的详细信息,例如消息或堆栈跟踪。

我是否在异常 class 中遗漏了一些对正确序列化很重要的东西?有没有更好的方法让 StackExchange.Precompilation 记录错误?


更新 如果我将抛出的异常类型更改为 ExceptionNotSupportedException 等标准,输出 window 将显示异常详细信息。但是,我仍然希望能够以这种方式使用自定义异常类型。

更新2 这个问题的答案揭示了为什么我的自定义异常类型不起作用。基本上,ASP.NET 编译器无法序列化不在 GAC 中的类型。

Type resolution error during ASP.NET precompilation

只需添加一个Diagnostic to the Diagnostics collections in Before/AfterCompileContext

另一种选择是 Console.WriteLineMsBuild VS-Aware error message format