첨부 소스 코드는 나눔고딕코딩 폰트를 사용합니다.
728x90
반응형
728x170

TestSolution.zip
다운로드

[TestAction 프로젝트]

▶ ILogHelper.cs

using System;

namespace TestAction
{
    /// <summary>
    /// 로그 헬퍼 인터페이스
    /// </summary>
    public interface ILogHelper
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Method

        #region 메시지 쓰기 - WriteMessage(message)

        /// <summary>
        /// 메시지 쓰기
        /// </summary>
        /// <param name="message">메시지</param>
        void WriteMessage(string message);

        #endregion
        #region 로그 쓰기 - WriteLog(format, parameterArray)

        /// <summary>
        /// 로그 쓰기
        /// </summary>
        /// <param name="format">포맷 문자열</param>
        /// <param name="parameterArray">매개 변수 배열</param>
        void WriteLog(string format, params object[] parameterArray);

        #endregion
        #region 에러 로그 쓰기 - WriteErrorLog(exception, caption)

        /// <summary>
        /// 에러 로그 쓰기
        /// </summary>
        /// <param name="exception">예외</param>
        /// <param name="caption">제목 문자열</param>
        void WriteErrorLog(Exception exception, string caption);

        #endregion
        #region 에러 로그 쓰기 - WriteErrorLog(exception, caption)

        /// <summary>
        /// 에러 로그 쓰기
        /// </summary>
        /// <param name="message">예외 메시지</param>
        /// <param name="caption">제목 문자열</param>
        void WriteErrorLog(string message, string caption);

        #endregion
        #region 덤프 로그 쓰기 - WriteDumpLog(sourceObject, caption)

        /// <summary>
        /// 덤프 로그 쓰기
        /// </summary>
        /// <param name="sourceObject">예외</param>
        /// <param name="caption">제목 문자열</param>
        void WriteDumpLog(object sourceObject, string caption);

        #endregion
    }
}

 

728x90

 

▶ FileLogHelper.cs

using System;
using System.IO;

namespace TestAction
{
    /// <summary>
    /// 파일 로그 헬퍼
    /// </summary>
    public class FileLogHelper : ILogHelper 
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Field
        ////////////////////////////////////////////////////////////////////////////////////////// Private

        #region Field

        /// <summary>
        /// 동기 객체
        /// </summary>
        private object syncObject;

        /// <summary>
        /// 로그 루트 디렉토리 경로
        /// </summary>
        private string logRootDirectoryPath;

        /// <summary>
        /// 파일명
        /// </summary>
        private string fileName;

        /// <summary>
        /// 파일 경로
        /// </summary>
        private string filePath;

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 생성자 - FileLogHelper(logRootDirectoryPath, fileName)

        /// <summary>
        /// 생성자
        /// </summary>
        /// <param name="logRootDirectoryPath">로그 루트 디렉토리 경로</param>
        /// <param name="fileName">파일명</param>
        public FileLogHelper(string logRootDirectoryPath, string fileName)
        {
            this.syncObject = new object();

            this.logRootDirectoryPath = logRootDirectoryPath;
            this.fileName             = fileName;

            this.filePath = Path.Combine(this.logRootDirectoryPath, this.fileName);

            if(!Directory.Exists(this.logRootDirectoryPath))
            {
                Directory.CreateDirectory(this.logRootDirectoryPath);
            }
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 메시지 쓰기 - WriteMessage(message)

        /// <summary>
        /// 메시지 쓰기
        /// </summary>
        /// <param name="message">메시지</param>
        public void WriteMessage(string message)
        {
            lock(this.syncObject)
            {
                using(StreamWriter writer = File.AppendText(this.filePath))
                {
                    writer.WriteLine(message);
                }
            }
        }

        #endregion
        #region 로그 쓰기 - WriteLog(format, parameterArray)

        /// <summary>
        /// 로그 쓰기
        /// </summary>
        /// <param name="format">포맷 문자열</param>
        /// <param name="parameterArray">매개 변수 배열</param>
        public void WriteLog(string format, params object[] parameterArray)
        {
            string message;

            if(parameterArray.Length == 0)
            {
                message = format;
            }
            else
            {
                message = string.Format(format, parameterArray);
            }

            string log = string.Format("[{0}] {1}", DateTime.Now.ToString("HH:mm:ss"), message);

            WriteMessage(log);
        }

        #endregion
        #region 에러 로그 쓰기 - WriteErrorLog(exception, source)

        /// <summary>
        /// 에러 로그 쓰기
        /// </summary>
        /// <param name="exception">예외</param>
        /// <param name="source">소스 문자열</param>
        public void WriteErrorLog(Exception exception, string source)
        {
            lock(this.syncObject)
            {
                string title = string.Format("[{0}] {1}", DateTime.Now.ToString("HH:mm:ss"), source);

                using(StreamWriter writer = File.AppendText(this.filePath))
                {
                    writer.WriteLine(title);
                    writer.WriteLine("--------------------------------------------------");
                    writer.WriteLine(exception.ToString());
                    writer.WriteLine("--------------------------------------------------");
                }
            }
        }

        #endregion
        #region 에러 로그 쓰기 - WriteErrorLog(message, source)

        /// <summary>
        /// 에러 로그 쓰기
        /// </summary>
        /// <param name="message">예외 메시지</param>
        /// <param name="source">소스 문자열</param>
        public void WriteErrorLog(string message, string source)
        {
            lock (this.syncObject)
            {
                string title = string.Format("[{0}] {1}", DateTime.Now.ToString("HH:mm:ss"), source);

                using (StreamWriter writer = File.AppendText(this.filePath))
                {
                    writer.WriteLine(title);
                    writer.WriteLine("--------------------------------------------------");
                    writer.WriteLine(message);
                    writer.WriteLine("--------------------------------------------------");
                }
            }
        }

        #endregion
        #region 덤프 로그 쓰기 - WriteDumpLog(sourceObject, caption)

        /// <summary>
        /// 덤프 로그 쓰기
        /// </summary>
        /// <param name="sourceObject">예외</param>
        /// <param name="caption">제목 문자열</param>
        public void WriteDumpLog(object sourceObject, string caption)
        {
            lock(this.syncObject)
            {
                string title = string.Format("[{0}] {1}", DateTime.Now.ToString("HH:mm:ss"), caption);

                using(StreamWriter writer = File.AppendText(this.filePath))
                {
                    writer.WriteLine(title);
                    writer.WriteLine("--------------------------------------------------");
                    writer.WriteLine(sourceObject.ToString());
                    writer.WriteLine("--------------------------------------------------");
                }
            }
        }

        #endregion
    }
}

 

300x250

 

▶ CustomAction.cs

using Microsoft.Deployment.WindowsInstaller;

namespace TestAction
{
    /// <summary>
    /// 커스텀 액션
    /// </summary>
    public class CustomAction
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Field
        ////////////////////////////////////////////////////////////////////////////////////////// Static
        //////////////////////////////////////////////////////////////////////////////// Private

        #region Field

        /// <summary>
        /// 버전
        /// </summary>
        private static string _version = "V1.0.0";

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Static
        //////////////////////////////////////////////////////////////////////////////// Public

        #region 최초 설치 시작 액션 실행하기 - ExecuteFirstInstallStartAction(session)

        /// <summary>
        /// 최초 설치 시작 액션 실행하기
        /// </summary>
        /// <param name="session">세션</param>
        /// <returns>액션 결과</returns>
        [CustomAction]
        public static ActionResult ExecuteFirstInstallStartAction(Session session)
        {
            ILogHelper logHelper = new FileLogHelper("d:\\", "TestNode.log");

            logHelper.WriteLog($"ExecuteFirstInstallStartAction : {_version}");

            return ActionResult.Success;
        }

        #endregion
        #region 최초 설치 종료 액션 실행하기 - ExecuteFirstInstallEndAction(session)

        /// <summary>
        /// 최초 설치 종료 액션 실행하기
        /// </summary>
        /// <param name="session">세션</param>
        /// <returns>액션 결과</returns>
        [CustomAction]
        public static ActionResult ExecuteFirstInstallEndAction(Session session)
        {
            ILogHelper logHelper = new FileLogHelper("d:\\", "TestNode.log");

            logHelper.WriteLog($"ExecuteFirstInstallEndAction : {_version}");

            return ActionResult.Success;
        }

        #endregion

        #region 업그레이드 설치 시작 액션 실행하기 - ExecuteUpgradeInstallStartAction(session)

        /// <summary>
        /// 업그레이드 설치 시작 액션 실행하기
        /// </summary>
        /// <param name="session">세션</param>
        /// <returns>액션 결과</returns>
        [CustomAction]
        public static ActionResult ExecuteUpgradeInstallStartAction(Session session)
        {
            ILogHelper logHelper = new FileLogHelper("d:\\", "TestNode.log");

            logHelper.WriteLog($"ExecuteUpgradeInstallStartAction : {_version}");

            return ActionResult.Success;
        }

        #endregion
        #region 업그레이드 설치 종료 액션 실행하기 - ExecuteUpgradeInstallEndAction(session)

        /// <summary>
        /// 업그레이드 설치 종료 액션 실행하기
        /// </summary>
        /// <param name="session">세션</param>
        /// <returns>액션 결과</returns>
        [CustomAction]
        public static ActionResult ExecuteUpgradeInstallEndAction(Session session)
        {
            ILogHelper logHelper = new FileLogHelper("d:\\", "TestNode.log");

            logHelper.WriteLog($"ExecuteUpgradeInstallEndAction : {_version}");

            return ActionResult.Success;
        }

        #endregion

        #region 설치 제거 시작 액션 실행하기 - ExecuteUninstallStartAction(session)

        /// <summary>
        /// 설치 제거 시작 액션 실행하기
        /// </summary>
        /// <param name="session">세션</param>
        /// <returns>액션 결과</returns>
        [CustomAction]
        public static ActionResult ExecuteUninstallStartAction(Session session)
        {
            ILogHelper logHelper = new FileLogHelper("d:\\", "TestNode.log");

            logHelper.WriteLog($"ExecuteUninstallStartAction : {_version}");

            return ActionResult.Success;
        }

        #endregion
        #region 설치 제거 종료 액션 실행하기 - ExecuteUninstallEndAction(session)

        /// <summary>
        /// 설치 제거 종료 액션 실행하기
        /// </summary>
        /// <param name="session">세션</param>
        /// <returns>액션 결과</returns>
        [CustomAction]
        public static ActionResult ExecuteUninstallEndAction(Session session)
        {
            ILogHelper logHelper = new FileLogHelper("d:\\", "TestNode.log");

            logHelper.WriteLog($"ExecuteUninstallEndAction : {_version}");

            return ActionResult.Success;
        }

        #endregion
    }
}

※ Wix Toolset와 Wix Toolset 비주얼 스튜디오 확장이 설치되어 있어야 한다.

728x90
반응형
그리드형(광고전용)
Posted by icodebroker

댓글을 달아 주세요