ITnef Compliance Logger Interface
An interface for recording TNEF compliance violations.
Definition
Namespace: MimeKit.Tnef
Assembly: MimeKit.Core (in MimeKit.Core.dll) Version: 5.0.0
Assembly: MimeKit.Core (in MimeKit.Core.dll) Version: 5.0.0
C#
public interface ITnefComplianceLoggerRemarks
Implementations of this interface are intended to capture and record information about TNEF compliance issues detected during parsing. This can be used for diagnostics, auditing, or reporting purposes in systems that process TNEF data.
No limit is placed by default on how many issues a single stream may produce. When the streams being parsed are untrusted, configure a per-violation limit on the reader to bound the report. An implementation that retains issues rather than summarizing them should impose its own limit as well, since it cannot assume that every caller configures one.
Example
C#
class TnefComplianceCollector : ITnefComplianceLogger
{
public List<TnefComplianceIssue> Issues { get; } = new List<TnefComplianceIssue> ();
public void Log (in TnefComplianceIssue issue)
{
Issues.Add (issue);
}
}
class StrictTnefComplianceLogger : ITnefComplianceLogger
{
public void Log (in TnefComplianceIssue issue)
{
// Reject anything that is likely to cause real problems; tolerate the rest.
if (issue.Severity >= MimeComplianceSeverity.Major)
throw new TnefException (issue.Violation, issue.Description);
}
}
public static TnefMessage LoadAndReportIssues (Stream stream)
{
var collector = new TnefComplianceCollector ();
TnefMessage tnef;
// The reader never throws for malformed TNEF; it recovers as best it can and reports each problem
// to the ComplianceLogger. MaxComplianceIssuesPerViolation limits how many times each kind of
// violation is reported.
using (var reader = new TnefReader (stream, null, leaveOpen: true) { ComplianceLogger = collector, MaxComplianceIssuesPerViolation = 16 })
tnef = TnefMessage.Load (reader);
foreach (var issue in collector.Issues)
Console.WriteLine ("{0} ({1}) @ offset {2}: {3}", issue.Violation, issue.Severity, issue.StreamOffset, issue.Description);
return tnef;
}
public static TnefMessage LoadStrict (Stream stream)
{
using (var reader = new TnefReader (stream, null, leaveOpen: true) { ComplianceLogger = new StrictTnefComplianceLogger () })
return TnefMessage.Load (reader);
}
Methods
| Log | Log a TNEF compliance violation. |