You are viewing an old version of this page. View the current version.

Compare with Current View Page History

Version 1 Next »

page under construction

Logging

There are 2 options for receiving the log information: subscribing to an event or using the LazyService from mst.NET. The later is the preferred one as it provides a weak connection to the library and minimizes the risk of objects remaining in memory by missing the unsubscribe.

Log messages via event

The event to be subscribed for receiving the log messages is:

Event for logging
				// Use event for receiving the log messages
				Messaging.MessagingCenter.LogItemCreated += MessagingCenter_LogItemCreated;

				[...]

				Messaging.MessagingCenter.LogItemCreated -= MessagingCenter_LogItemCreated;

Log messages via LazyService

Using this option requires the nuget package of mst.NET v1.1.0

Below there is an example on how to use this functionality

Log via LazyService
    	private static void Main(string[] args)
		{
 				// initialization of the application
				_working = true;

				// or use the lazy service provided by mst.NET
				Messaging.MessagingCenter.SetLoggingType(true);
				ServiceLocator.SetLocatorProvider(GetServiceLocator);
				MessageConsumerTask = Task.Factory.StartNew(ProcessNewMessages);

				[...]

				// end of application
				_working = false;
				MessageConsumerTask.Wait();
 		}

        #region use LazyService from mst.NET
 		static private bool _working;
		static private Task MessageConsumerTask;
		static LazyService<ILogger> s_loggerService = new LazyService<ILogger>();
		private static ServiceLocatorImpl s_serviceLocator; 

  		static private void ProcessNewMessages()
		{
			LogItem newLogItem;

			while (_working || !s_loggerService.Value.GeneralMessages.IsEmpty)
			{
				try
				{
					if (s_loggerService.Value.GeneralMessages.IsEmpty)
						Thread.Sleep(1);
					else if (s_loggerService.Value.GeneralMessages.TryDequeue(out newLogItem))
					{
						try
						{
							switch (newLogItem.LogLevel)
							{
								case LoggingLevel.Info:
									Console.WriteLine(newLogItem.LogLevel + "\t" + newLogItem.Message);
									break;
								case LoggingLevel.ExceptionsErrors:
									Console.WriteLine(newLogItem.LogLevel + "\t" + newLogItem.Message);
									break;
								case LoggingLevel.Debug:
									Console.WriteLine(newLogItem.LogLevel + "\t" + newLogItem.Message);
									break;
							}
						}
						catch (Exception except)
						{
							Console.WriteLine(except.Message + Environment.NewLine + except.StackTrace);
						}
					}
				}
				catch (Exception except)
				{
					Console.WriteLine(except.Message + Environment.NewLine + except.StackTrace);
				}
			}
		}
	

		/// <summary>
		/// Initializes all Application services
		/// </summary>
		private static IServiceLocator GetServiceLocator()
		{
			if (s_serviceLocator == null)
			{
				s_serviceLocator = new ServiceLocatorImpl();
				s_serviceLocator.Add<ILogger>(new H5MNETLogger());
			}
			return s_serviceLocator;
		}
		#endregion


Close file

The closing of the h5m file is done via the dispose methods. To ensure that the file is closed, the Dispose method should be called explicitly.

  • No labels