This project has moved. For the latest updates, please go here.

Custom reports

ReportGenerator supports several output formats out of the box. Starting with version 2.0 you are able to add custom output formats without modifing the source code of ReportGenerator. Additional formats can be supplied as MEF plugins

Basics

To create a custom report format you have to place a DLL, which contains one or multiple implementations of the IReportBuilder interface, in the installation directory of ReportGenerator.

The interface has the following properties and methods:
  • string ReportType { get; }
The name of the report format. Use this name to create a report in the corresponding format by passing -reporttypes:MyFormat as a commandline argument.
If you use the same name as one of the formats that come with ReportGenerator, then your implemention replaces the default one.
  • string TargetDirectory { get; set; }
The directory where the report file(s) should be saved
  • void CreateClassReport(Class @class, IEnumerable<FileAnalysis> fileAnalyses)
This method is called for every covered class in the coverage report(s). The classes are sorted by assembly then by their name.
  • void CreateSummaryReport(SummaryResult summaryResult)
This method is called at the end and the summarized results are supplied.
Attention: This method is called after all CreateClassReport invocations. If you create a single file report you have to place the summary results at the correct position yourself.

Example

In this step-by-step sample the report is generated in CSV format.
  • Create a new Class Library project in Visual Studio
  • Add a reference to ReportGenerator.exe and to System.ComponentModel.Composition
  • Create a new class that implements the IReportBuilder interface.
  • Add the following implementation (or change it as you like):
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using Palmmedia.ReportGenerator.Parser.Analysis;
using Palmmedia.ReportGenerator.Reporting;

[System.ComponentModel.Composition.Export(typeof(IReportBuilder))]
public class CsvSummaryReportBuilder : IReportBuilder
{
    public string ReportType
    {
        get { return "CsvSummary"; }
    }

    public string TargetDirectory { get; set; }

    public void CreateClassReport(Class @class, IEnumerable<FileAnalysis> fileAnalyses)
    {
    }

    public void CreateSummaryReport(SummaryResult summaryResult)
    {
        string targetPath = Path.Combine(this.TargetDirectory, "Summary.csv");

        using (var reportTextWriter = new StreamWriter(new FileStream(targetPath, FileMode.Create), Encoding.UTF8))
        {
            foreach (var assembly in summaryResult.Assemblies)
            {
                reportTextWriter.WriteLine(
                    "{0};{1}",
                    assembly.Name,
                    assembly.CoverageQuota.HasValue ? assembly.CoverageQuota.Value.ToString("f1", CultureInfo.InvariantCulture) + "%" : string.Empty);

                if (assembly.Classes.Any())
                {
                    reportTextWriter.WriteLine();
                }

                foreach (var @class in assembly.Classes)
                {
                    reportTextWriter.WriteLine(
                        "{0};{1}",
                        @class.Name,
                        @class.CoverageQuota.HasValue ? @class.CoverageQuota.Value.ToString("f1", CultureInfo.InvariantCulture) + "%" : string.Empty);
                }
            }
        }
    }
}

Attention: Don't forget to add the Export attribute, otherwise ReportGenerator will not recognize your report builder.
  • Compile
  • Drop the DLL in the installation directory of ReportGenerator.

Download: Sample project

Advanced

  • The class Palmmedia.ReportGenerator.Properties.ReportResources already defines some resource strings. You may want to reuse them in your reports.
  • The sample above example is relatively simple. You may also have a look the default IReportBuilders implementations. Most of them are based on ReportBuilderBase, which uses an IReportRenderer implementation.

Attention: The report builders in IReportGenerator.Reporting may change in a future release, this may break your code (if you derive from those classes).
The interface IReportBuilder will (most likely) not change.

Last edited Jan 6, 2015 at 10:28 AM by danielpalme, version 18