DLL无法写入文件,因为它正在被另一个进程使用

时间:2019-01-08 19:46:13

标签: c# excel vba

我有一个正在使用的C#库,可以在Excel电子表格中从VBA调用它。我一直想将Logging功能添加到库中,所以我创建了可以从VBA初始化的Logger类。

using System;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;

/// <summary>
/// Logger - Logging class for applications
/// </summary>
[Serializable(), ClassInterface(ClassInterfaceType.AutoDual), ComVisible(true)]
public class Logger
{
    #region Constructors
    /// <summary>
    /// Initializes a new instance of the <see cref="Logger"/> class
    /// </summary>
    public Logger()
    {
    }

    /// <summary>
    /// Initializes a new instance of the <see cref="Logger"/> class
    /// </summary>
    /// <param name="logPath">Path to write to</param>
    /// <param name="message">Initial message</param>
    public Logger(string logPath, string message)
    {
        this.Initialize(logPath, message);
    }
    #endregion

    #region Properties
    /// <summary>
    /// Gets or sets a value indicating whether the logger is active
    /// </summary>
    public bool Active { get; set; }

    /// <summary>
    /// Gets or sets the path for the Logger to use
    /// </summary>
    public string LogPath { get; set; }
    #endregion

    #region Methods
    /// <summary>
    /// Initializes the logger. This method is for VBA use
    /// </summary>
    /// <param name="logPath">Path to write to</param>
    /// <param name="message">Initial Message</param>
    public void Initialize(string logPath, string message)
    {
        this.Activate(logPath);
        this.ClearLog();
        this.LogWrite(message);
    }

    /// <summary>
    /// Writes the log
    /// </summary>
    /// <param name="logMessage">Message to send</param>
    public void LogWrite(string logMessage)
    {
        if (this.IsActive())
        {
            try
            {
                using (StreamWriter writer = File.AppendText(this.LogPath))
                {
                    Log(logMessage, writer);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }

    /// <summary>
    /// Writes the Log entry
    /// </summary>
    /// <param name="logMessage">Message to use</param>
    /// <param name="writer"></param>
    public void Log(string logMessage, TextWriter writer)
    {
        try
        {
            writer.Write("\nLog Entry [{0} (1}] ", DateTime.Now.ToLongDateString(), DateTime.Now.ToLongTimeString());
            writer.Write(":: {0}", logMessage);
        }
        catch (Exception e)
        {
            this.WriteErrorMessage(e);
        }
    }

    /// <summary>
    /// Writes a success message to the log
    /// </summary>
    /// <param name="message">Message to send</param>
    public void WriteSuccessMessage(string message)
    {
        if (this.IsActive())
        {
            try
            {
                using (StreamWriter writer = File.AppendText(this.LogPath))
                {
                    Log("***SUCCESS: " + message + "***", writer);
                }
            }
            catch (Exception e)
            {
                this.WriteErrorMessage(e);
            }
        }
    }

    /// <summary>
    /// Handles logging of Error Messages
    /// </summary>
    /// <param name="e">Exception to pass</param>
    /// <param name="message">Message to send</param>
    public void WriteErrorMessage(Exception e, string message = null)
    {
        try
        {
            StringBuilder errorMessage = new StringBuilder();
            errorMessage.Append("***ERROR");
            if (!string.IsNullOrWhiteSpace(message)) errorMessage.Append(" [" + message + "]");
            errorMessage.Append("***\n");
            errorMessage.Append("Exception Message: " + e.Message);
            errorMessage.Append("\n\tStack Trace:\n\t\t" + e.StackTrace);
            errorMessage.Append("\n\t\tInner Exception\n\t\t\t" + e.InnerException);
            errorMessage.Append("\n***END ERROR***");
            this.LogWrite(errorMessage.ToString());
        }
        catch (Exception)
        {
            throw;
        }
    }

    /// <summary>
    /// Clears the file of any residual logging
    /// </summary>
    public void ClearLog()
    {
        if (this.IsActive())
        {
            try
            {
                File.WriteAllText(this.LogPath, String.Empty);
            }
            catch(Exception e)
            {
                this.WriteErrorMessage(e);
            }
        }
    }

    /// <summary>
    /// Checks to make sure the Logger is active by having both the <see cref="Active"/> flag true and having a Logging path
    /// </summary>
    /// <returns><see cref="bool"/></returns>
    private bool IsActive() => this.Active && !string.IsNullOrWhiteSpace(this.LogPath);

    /// <summary>
    /// Activates the Logger
    /// </summary>
    /// <param name="logPath">Path to use</param>
    private void Activate(string logPath)
    {
        this.Active = true;
        this.LogPath = logPath;
    }
    #endregion
}

我在其他应用程序中使用了此类,并且效果很好。但是,当我尝试从VBA中初始化Logger时,出现错误:

Global Log as Logger
Public Sub InitializeWorkbook()

    Set Log = New Logger
    Log.Initialize "\\myPath\Log.txt", "Logging is turned on"

End Sub
  

该进程无法访问文件'\ myPath \ Log.txt',因为它正在被另一个进程使用

Logger类可以创建文本文件,但是我无法克服错误。

0 个答案:

没有答案