Logging Metrics

  • Updated

Prerequisites: A valid RuleSession
Namespaces: InRule.Runtime
Classes: IMetricLogger, RuleSession
See Also: Retrieving a Rule Application, Creating a RuleSession

To handle the multitude of options for desired logging locations, InRule has implemented an adaptor based model. An adaptor is a .NET assembly that is available to the rule engine that implements the IMetricLogger interface. When this assembly exists, the engine will call out to the required methods in the assembly to perform the actual logging. This provides customers the ability to write metrics to any location that is required in their implementation.

At the time of this writing, there are 3 adaptors that are available in GitHub.

  • Microsoft® Azure® Table Storage
  • SQL Server
  • CSV (primarily for demo purposes)

To enable metrics logging

You must set the MetricLogger property in the Rule Session Settings to an instance of an object that implements the IMetricLogger interface.

ruleSession.Settings.MetricLogger = new CsvMetricLogger();

Example of a Metrics Logger which logs to a CSV file

public sealed class CsvMetricLogger : IMetricLogger
  public async Task LogMetricsAsync(string serviceId, string ruleApplicationName, Guid sessionId, Metric[]
      // for async sample see the Azure Table Storage Adaptor
      throw new NotImplementedException();

  public void LogMetrics(string serviceId, string ruleApplicationName, Guid sessionId, Metric[] metrics)
      // loop through all of the metrics that are emitted by the rules engine
      // there will be one metric per entity
      foreach (Metric metric in metrics)

          // get a list of the field and/or rule names
          var fields = new List<string>();
          foreach (var metricProperty in metric.Schema.Properties)

          // get the value of each field or rule
          var values = new List<string>();
          foreach (var metricProperty in metric.Schema.Properties)

          // save them to disk in a csv file
           SaveToFile(fields, values, metric.EntityName);

  private void SaveToFile(List<string> fields, List<string> values, string metricEntityName)
      var fileName = $"{ConfigurationManager.AppSettings["OutputDirectory"]}{metricEntityName}.csv";

      if (File.Exists(fileName))

          // if the file already exists, append the values
           File.AppendAllText(fileName, string.Join(",", values.ToArray()) + Environment.NewLine);
          // if the file doesn't exist, create it and include the headers
          var s = new StringBuilder();
           s.AppendLine(string.Join(",", fields.ToArray()));
           s.AppendLine(string.Join(",", values.ToArray()));
           File.WriteAllText(fileName, s.ToString());

For more details, refer to the CSV metrics logger sample application.

Was this article helpful?

0 out of 0 found this helpful



Please sign in to leave a comment.