// WS-POS version 1.3 参照実装
// 
// 実装 version 0.6 

using System;
using System.Diagnostics;
using System.Reflection;
using System.Threading;

namespace WSPOSInitiative.Common
{
    public class LogServiceEventLogImpl : ILog
    {
        private const string WSPOSServiceLogSourceName = "WS-POS Behavior";

        EventLog _eventLog;

        /// <summary>
        /// コンストラクタ
        /// </summary>
        public LogServiceEventLogImpl()
        {
            if (!EventLog.SourceExists(WSPOSServiceLogSourceName))
            {
                EventLog.CreateEventSource(WSPOSServiceLogSourceName, "Application");
            }
            _eventLog = new EventLog();
            _eventLog.Source = WSPOSServiceLogSourceName;
        }

        /// <summary>
        /// ログ出力
        /// </summary>
        /// <param name="currentMethod">呼び出したメソッド</param>
        /// <param name="message">出力文字列</param>
        public virtual void LogWriteLineMessage(MethodBase currentMethod, string message)
        {
            string logString = string.Format("{0:yyyy/MM/dd HH:mm:ss.fffffff} {1:000000000} {2}.{3}#{4} {5}", DateTime.Now, Thread.CurrentThread.ManagedThreadId, currentMethod.DeclaringType.Namespace, currentMethod.DeclaringType.Name, currentMethod.Name, message);
            Debug.WriteLine(logString);
            _eventLog.WriteEntry(logString);
        }

        /// <summary>
        /// ログ出力
        /// </summary>
        /// <param name="currentMethod">呼び出したメソッド</param>
        /// <param name="ex">例外</param>
        public virtual void LogWriteLineException(MethodBase currentMethod, Exception ex)
        {
            string logString = string.Format("{0:yyyy/MM/dd HH:mm:ss.fffffff} {1:000000000} {2}.{3}#{4} Exception thrown = {5}", DateTime.Now, Thread.CurrentThread.ManagedThreadId, currentMethod.DeclaringType.Namespace, currentMethod.DeclaringType.Name, currentMethod.Name, ex);
            Debug.WriteLine(logString);
            _eventLog.WriteEntry(logString, EventLogEntryType.Error);
        }

        /// <summary>
        /// ログ出力
        /// </summary>
        /// <param name="currentMethod">呼び出したメソッド</param>
        /// <param name="ex">例外</param>
        public virtual void LogWriteLineException(MethodBase currentMethod, Exception ex, string message)
        {
            string logString = string.Format("{0:yyyy/MM/dd HH:mm:ss.fffffff} {1:000000000} {2}.{3}#{4} {6} Exception thrown = {5}", DateTime.Now, Thread.CurrentThread.ManagedThreadId, currentMethod.DeclaringType.Namespace, currentMethod.DeclaringType.Name, currentMethod.Name, ex, message);
            Debug.WriteLine(logString);
            _eventLog.WriteEntry(logString, EventLogEntryType.Error);
        }
    }
}
