SCSF: Append Messages into LogView. using Event publication

509 views Asked by At

I created a new View (LogView) in Infrastructure.Module project. This view will be used as LogViewer like output window in VS. i want to write different status messags in this LogView from different modules.

I also created a class LogWriter which is publishing an event to write message into LogView

i am facing problem to access this LogWriter class in my whole application.. please tell me how can i use this...

public class LogWriter 
    {
        [EventPublication(EventTopicNames.WriteToLog, PublicationScope.Global)]
        public event EventHandler<EventArgs<string>> WriteToLog;

        private void OnWriteToLog(EventArgs<string> eventArgs)
        {
            if (WriteToLog != null)
            {
                WriteToLog(null, eventArgs);
            }
        }

        public void WriteMsg(string msg)
        {
            OnWriteToLog(new EventArgs<string>(msg));
        }
    }

and in LogView event subscription is

 [EventSubscription(EventTopicNames.WriteToLog, ThreadOption.UserInterface)]
        public void OnWriteToLog(object sender, EventArgs<string> eventArgs)
        {
            this.txtLogs.AppendText(eventArgs.Data + Environment.NewLine);
        }

please suggest me a solution

LogWriter class is in Infrastructure.Interface project LogViewer is in Infrastructure.Module project

In ModuleController.cs of Infrastructure.Module i Added LogWriter in WorkItem.Services Collection

 WorkItem.Services.AddNew<LogWriter>();

and in one other project i am getting it using

var logWriter = WorkItem.Services.Get(); if (logWriter != null) logWriter.WriteMsg("message");

but it is returning me null.

module loading sequence is also correct.

1

There are 1 answers

6
Tom Anderson On BEST ANSWER

Add this attribute to your LogWriter class

[Service(typeof(LogWriter), AddOnDemand=true)]
public class LogWriter
{
    ...
}

then in your code simply access it by doing this:

var logWriter = WorkItem.Service.Get<LogWriter>();
if (logWriter != null)
    logWriter.WriteMsg("message");

This will require that the Module that the LogWriter is in is loaded before the one that tries to access it. I would recommend adding a module dependency if they are seperated. The exact way to do it depends on the IModuleLoader you are using.