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 to a 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 to it:
var soundListener = new SoundLogListener("error.mp3"); logManager.Listeners.Add(soundListener); logManager.Sources.Add(lkohSmaStrategy);
Setting the filter so sound plays only when the message type is LogLevels.Error:
soundListener.Filters.Add(msg => msg.Level == LogLevels.Error);
Email sending
Create the logger and pass it the parameters for the sent letters:
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
Creating the GuiLogListener logger:
// each strategy will have their own windows var guiListener = new GuiLogListener(); logManager.Listeners.Add(guiListener); logManager.Sources.Add(lkohSmaStrategy);
Here is the log window when the strategy is working: