Skip to content

Custom reports

Daniel Palme edited this page Nov 3, 2018 · 14 revisions

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

Depending on the version of ReportGenerator you have to follow the corresponding instructions:

Version 4.x

Basics

To create a custom report format you have to create a DLL, which contains one or multiple implementations of the IReportBuilder interface.
In order to let ReportGenerator know about the DLL, you have to pass the path of the DLL to ReportGenerator via the -plugins command line parameter.
ReportGenerator supports both the full .NET Framework and .NET Core. If you implement your plugin as a .NET Standard 2.0 library, your plugin will work in both environments.

The IReportBuilder 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 Output formats that come with ReportGenerator, then your implementation replaces the default one.

  • IReportContext ReportContext { get; set; }
    Contains information about the report configuration. E.g. 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 .NET Standard 2.0 Library project in Visual Studio
  • Add the ReportGenerator.Core Nuget package to your project
  • 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.Core.Parser.Analysis;
using Palmmedia.ReportGenerator.Core.Reporting;

namespace MyCompany.CustomReportTypes
{
    public class CsvSummaryReportBuilder : IReportBuilder
    {
        public string ReportType => "CsvSummary";

        public IReportContext ReportContext { get; set; }

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

        public void CreateSummaryReport(SummaryResult summaryResult)
        {
            string targetPath = Path.Combine(this.ReportContext.ReportConfiguration.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);
                    }
                }
            }
        }
    }
}
  • Compile
  • Pass the path of the DLL to ReportGenerator via the -plugins command line parameter

Download: Sample project (Version 4.x)

Advanced

  • The class ReportResources already defines some resource strings. You may want to reuse them in your reports.
  • The example above is relatively simple. You may also have a look the default IReportBuilders implementations.
    Some 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.

Version 3.x

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 Output formats that come with ReportGenerator, then your implementation replaces the default one.

  • IReportConfiguration ReportConfiguration { get; set; }
    Contains information about the report configuration. E.g. 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 IReportConfiguration ReportConfiguration { get; set; }

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

    public void CreateSummaryReport(SummaryResult summaryResult)
    {
        string targetPath = Path.Combine(this.ReportConfiguration.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 (Version 3.x)

Advanced

  • The class Palmmedia.ReportGenerator.Properties.ReportResources already defines some resource strings. You may want to reuse them in your reports.
  • The example above 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.

You can’t perform that action at this time.