Skip to main content

Logging

Access it through DarraEtherCAT.Logs.​

Runtime logs are pushed to DarraEtherCAT.Logs (a read-only list) through a callback that Build() registers automatically.

Quick start​

// bind to console output
DarraEtherCAT.Logs.Updated += () =>
{
var logs = DarraEtherCAT.Logs;
if (logs.Count > 0)
Console.WriteLine(logs[logs.Count - 1]);
};

// show errors and warnings only
DarraEtherCAT.Logs.SetFilter(LogCategory.Error, LogCategory.Warning);

// enable debug logging (off by default)
DarraEtherCAT.EnableDebugLogging();

Log categories​

public enum LogCategory
{
Error = 0, // system errors and exceptions
Warning = 1, // potential problems
Message = 2, // general information (state changes and so on)
Mailbox = 3, // CoE/SoE/FoE/EoE mailbox communication (off by default)
PDO = 4, // PDO data-change log (off by default)
Debug = 5, // detailed debug information (Debug builds only, nothing is logged in Release)
Local = 6 // console output captured from Console.WriteLine
}
PDO log

The PDO log is written only when PDO data changes; it does not produce an entry every cycle. It is useful for debugging PDO mapping problems.

Properties​

DarraEtherCAT.Logs​

public static LogView Logs { get; }

Live log view (a read-only list), maintained automatically by the logging system.

Related structures:

public class LogEntry
{
public DateTime Timestamp { get; } // log timestamp
public LogCategory Category { get; } // log category
public string Message { get; } // log message text
}

Logs.Updated​

public event Action Updated;

Raised when the log data changes (new entries / clearing / filter changes).

Logs.Retain​

public bool Retain { get; set; }  // default true

Log retention switch. true (the default) accumulates entries up to the limit; false clears the log automatically before every key operation.

Capacity and retention policy

The log list is capped at 10000 entries; past that the oldest entries are dropped automatically (FIFO).

Retention switch Logs.Retain (default true): by default the log accumulates (it is not cleared before key operations); when set to false, every key operation (Build / SetState / SDO and so on) clears the log before it starts, so only the entries of the current operation remain.

Filtering​

SetFilter(params LogCategory[] categories)​

public void SetFilter(params LogCategory[] categories)

Set the filter so that only entries of the given categories are shown. This replaces the previous filter settings.

Default filter: Error + Warning (other categories appear only after SetFilter extends the filter).

Parameters:

  • categories (LogCategory[]) — the log categories to show

Example:

// show errors and warnings only
DarraEtherCAT.Logs.SetFilter(LogCategory.Error, LogCategory.Warning);

// switch to mailbox communication only (replaces the filter directly)
DarraEtherCAT.Logs.SetFilter(LogCategory.Mailbox);

// show all categories (pass every enum value)
DarraEtherCAT.Logs.SetFilter(
LogCategory.Error, LogCategory.Warning, LogCategory.Message,
LogCategory.Mailbox, LogCategory.PDO, LogCategory.Debug, LogCategory.Local);

GetAll()​

public List<LogEntry> GetAll()

Get every log entry (ignoring the filter). Use it for export or custom filtering.

Log switches​

The Mailbox and PDO logs are off by default; enable them manually when needed. The Debug category takes effect in Debug builds only.

DarraEtherCAT.InitializeLogging()​

public static void InitializeLogging()

Start the logging system explicitly; this registers the log callback and the crash callback. Build() calls it automatically, so you usually do not need to call it yourself; call it once only when you need to see logs before Build(). Repeated calls have no side effects.

LogManager.Enabled / FileOutputEnabled​

using DarraEtherCAT_Master;

LogManager.Instance.Enabled = false; // master log switch, default true
LogManager.Instance.FileOutputEnabled = false; // file output switch, default true
  • Enabled = false: AddLog returns early across the board — nothing goes into the in-memory view and no file is written; event dispatch is unaffected
  • FileOutputEnabled = false: nothing is written to events.log / pdo_trace.txt / mailbox_trace.txt; the in-memory view and the events continue as usual
  • Both can be changed at any time at runtime; a GUI-less acquisition process that only wants the disk kept quiet can simply turn FileOutputEnabled off

DarraEtherCAT.EnablePDOLogging(bool enable = true)​

public static void EnablePDOLogging(bool enable = true)

Enable the PDO log. Entries are written only when PDO data changes. This affects real-time performance, so use it with care.

DarraEtherCAT.EnableMailboxLogging(bool enable = true)​

public static void EnableMailboxLogging(bool enable = true)

Enable the mailbox log (CoE/SoE/FoE/EoE mailbox communication logging).

DarraEtherCAT.EnableDebugLogging(bool enable = true)​

public static void EnableDebugLogging(bool enable = true)

Enable debug logging (detailed debug information).

Full example​

// 1. bind console output (show every log entry in real time)
DarraEtherCAT.Logs.Updated += () =>
{
var logs = DarraEtherCAT.Logs;
if (logs.Count > 0)
Console.WriteLine(logs[logs.Count - 1]);
};

// 2. enable debug logging
DarraEtherCAT.EnableDebugLogging();
DarraEtherCAT.EnableMailboxLogging();

// 3. filter logs (errors and debug only)
DarraEtherCAT.Logs.SetFilter(LogCategory.Error, LogCategory.Debug);

// 4. export all logs (ignoring the filter)
var allLogs = DarraEtherCAT.Logs.GetAll();
foreach (var entry in allLogs)
System.IO.File.AppendAllText("debug.log", entry.ToString() + "\n");
WinForms / WPF binding

Logs.Updated is raised on a non-UI thread, so UI updates must be marshalled:

// WinForms
DarraEtherCAT.Logs.Updated += () =>
this.Invoke(() => listBox.Items.Add(DarraEtherCAT.Logs[DarraEtherCAT.Logs.Count - 1]));

// WPF
DarraEtherCAT.Logs.Updated += () =>
Dispatcher.Invoke(() => LogItems.Add(DarraEtherCAT.Logs[DarraEtherCAT.Logs.Count - 1]));