How to release Logger file handler when using with

2019-07-31 02:38发布

问题:

First, I followed the instructions as stated here

Everything works well. Except one thing when i deployed to Azure. When there is logging taking place, the file created seems to be 'locked' by the process, such that I am unable to 'transfer the file' over to my local.

When this happens, the failure takes place forever, until i manually stop my api process, then only i am able to transfer the file.

Question: How to properly write the code to 'release the file lock'?

Below shows my code snippet:

        private static LogWriter logWriter;
        private static readonly ExceptionPolicyFactory _exceptionPolicyFactory;
        private static readonly ExceptionManager _exceptionManager;

        static CustomExceptionHandler()
        {
            logWriter = new LogWriterFactory().Create();
            Logger.SetLogWriter(logWriter, false);

            _exceptionPolicyFactory = new ExceptionPolicyFactory();
            _exceptionManager = _exceptionPolicyFactory.CreateManager();
        }

        public static void HandleAPIFilterException(Exception ex)
        {
            Exception exceptionToThrow = null;

            //The HandleException method will return true if the exception should be (re-)thrown.
            if (_exceptionManager.HandleException(ex, "MY-API-Filter", out exceptionToThrow))
            {
                if (exceptionToThrow == null)
                {
                }
                else
                {
                }
            }
        }