Strategy logging
The Strategy class implements the ILogSource interface. Therefore, the strategies can be passed to the LogManager.Sources, and all of its messages will automatically get to the LogManager.Listeners.
Prerequisites
Logging in to test file
First, you need to create the special manager:
var logManager = new LogManager();
Then you need to create a file logger, passing to it the name of the file and to add it to the LogManager.Listeners:
var fileListener = new FileLogListener("{0}_{1:00}_{2:00}.txt".Put(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day)); logManager.Listeners.Add(fileListener);
For logging messages, you need to add a strategy to the LogManager.Sources:
logManager.Sources.Add(lkohSmaStrategy);
After adding strategy to the logging manager all of its messages will be recorded to file.
Sound playback
Creating a logger and passing the name of the sound file in it:
var soundListener = new SoundLogListener("error.mp3"); logManager.Listeners.Add(soundListener); logManager.Sources.Add(lkohSmaStrategy);
Setting the filter in the sound playback only when the type of messages is LogLevels.Error:
soundListener.Filters.Add(msg => msg.Level == LogLevels.Error);
Email sending
The logger creating and passing to it sent letters parameters:
var emailListener = new EmailLogListener("from@stocksharp.com", "to@stocksharp.com"); logManager.Listeners.Add(emailListener); logManager.Sources.Add(lkohSmaStrategy);
Setting the filter on the sending of messages of LogLevels.Error and LogLevels.Warning types:
emailListener.Filters.Add(msg => msg.Level == LogLevels.Error); emailListener.Filters.Add(msg => msg.Level == LogLevels.Warning);
Logging in to the LogWindow
The GuiLogListener logger creating:
// each strategy will have they own windows var guiListener = new GuiLogListener(); logManager.Listeners.Add(guiListener); logManager.Sources.Add(lkohSmaStrategy);
Here is the log window when strategy working: