【问题标题】:Implementing Non blocking thread safe singleton logging class [closed]实现非阻塞线程安全的单例日志记录类[关闭]
【发布时间】:2017-12-05 00:39:08
【问题描述】:

我想编写一个记录器类,它根据用户操作将输出写入日志文件。为此,我编写了一个 logmessage 类。以下是我所拥有的。

public class LogMessage
{
    private  static Object padlock = new Object();
    private static string serviceDirectory ="C:\\MySite\\";
    private static string filePath = "\\Logs\\Combined\\ActivityMonitorLog-" + DateTime.Now.ToString("dd'-'MM'-'yyyy") + ".log";

    private static volatile LogMessage instance = new LogMessage();
    public static LogMessage Instance
    {
        get
        {
            if (instance == null)
            {
                lock (padlock)
                {
                    if (instance == null)
                        instance = new LogMessage();
                }
            }

            return instance;
        }
    }


    public void SaveLogMessage(string userName, string message, string stackTrace)
    {
        lock (padlock)
        {
            using (StreamWriter logWriter =
                new StreamWriter(serviceDirectory + filePath, true))
            {
                string logMessage = "";
                if (!string.IsNullOrEmpty(stackTrace))
                    logMessage = string.Format("{0} >> user : {1} ::: {2} ::: stack trace ::: {3}", DateTime.Now.ToString(), userName, message,stackTrace);
                else
                    logMessage = string.Format("{0} >> user : {1} ::: {2}", DateTime.Now.ToString(), userName, message);

                logWriter.WriteLine(logMessage);

            }
        }
    }
}

当我想做一个日志条目时,我会调用下面的 about 方法

LogMessage.Instance.SaveLogMessage("My User", "Enter Logout PageLoad : Current Method :" + "Page_Load @ Logout.aspx.cs", "");

我相信我已经实现了单例部分和线程安全。但我不认为这是非阻塞的,因为多个用户同时登录,每个用户都必须等到文件可以写入。我正在考虑使用 backgroundworker 从 LogMessage 单例对象调用此 SaveLogMessage() 。这是正确的做法吗?

更新

我已经使用 backgroundworker 和完整的类实现了这一点,答案中提到了如何调用。感谢@bret 和@huan 的投入

【问题讨论】:

  • 无法回答您尚未编写的代码是否正确。显然,从教育的角度来看,尝试使用后台工作人员是“正确的方式”,因为您将学习新事物。从实际的角度来看,您只需使用现有的经过验证的日志库并使用它...所以这是我的意见 - 是的,这是正确的方法。

标签: c# asp.net multithreading thread-safety nonblocking


【解决方案1】:

对于您的“实例”字段,请使用“只读”而不是“易失性”,因为该字段的值永远不会改变。

您甚至不需要公共 Instance 属性——您可以将 SaveLogMessage 设为静态方法并直接使用它。

在 SaveLogMessage 正文中,您可以使用 Task.Run(() => { ... }) 在后台线程上执行日志记录操作以使其非阻塞。如果您非常频繁地登录,这可能不是非常有效。

考虑改用 System.Diagnostics.Trace,这会为您提供更大的灵活性。

【讨论】:

    【解决方案2】:
    1. 尝试写入时最好使用 ReaderWriterLockslim 仅添加写入锁。
    2. 我认为使用 ReaderWriterLockslim 的静态方法就足够了。 有个好帖子here
    3. 如果你想在这里使用单例,你可以使用 Lazy 参考Implementing the Singleton Pattern in C#

    顺便说一句,为什么不使用log4net 或任何其他现有库。

    【讨论】:

      【解决方案3】:

      最后,我设法通过使用后台工作人员实现了一个非阻塞、线程安全的单例日志记录类。我正在提交我的解决方案,以防有一天有人发现它有用。

      using System;
      using System.Collections.Generic;
      using System.IO;
      using System.Web;
      using System.Threading;
      using System.Threading.Tasks;
      using System.ComponentModel;
      using System.Diagnostics;
      using System.Web.Configuration;
      /// <summary>
      /// Summary description for LogMessage
      /// </summary>
      public class LogMessage
      {
          static ReaderWriterLock locker = new ReaderWriterLock();
      
          private static string serviceDirectory = HttpContext.Current != null ?
              AppDomain.CurrentDomain.BaseDirectory :
              Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
          private static string fullpath = serviceDirectory + "\\ActivityLog.log";
          private static readonly LogMessage instance = new LogMessage();
      
          public static LogMessage Instance
          {
              get { return instance; }
          }
      
          public void SaveLogMessage(string userName, string message, string stackTrace, bool inOut)
          {
              bool EnableActivityLogging = false;
      
              if (string.IsNullOrEmpty(WebConfigurationManager.AppSettings["EnableActivityLogging"]))
                  return;
      
              EnableActivityLogging = Convert.ToBoolean(WebConfigurationManager.AppSettings["EnableActivityLogging"]);
      
              if (!EnableActivityLogging)
                  return;
      
              BackgroundWorker logbw = new BackgroundWorker();
              logbw.DoWork += logbw_DoWork;
              logbw.RunWorkerCompleted += logbw_RunWorkerCompleted;
      
              List<string> paramList = new List<string>();
              paramList.Add(userName);
              paramList.Add(message);
              paramList.Add(stackTrace);
              paramList.Add(inOut.ToString());
      
              if (!logbw.IsBusy)
              {
                  logbw.RunWorkerAsync(paramList);
              }
          }
      
          void logbw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
          {
              Debug.Write("Log Message Background Worker is now free...");
          }
      
          void logbw_DoWork(object sender, DoWorkEventArgs e)
          {
              List<string> paramList = (List<string>)e.Argument;
              string userName = paramList[0].ToString();
              string message = paramList[1].ToString();
              string stackTrace = paramList[2].ToString();
              bool inOut = bool.Parse(paramList[3].ToString());
      
              try
              {
                  locker.AcquireWriterLock(int.MaxValue);
                  using (StreamWriter logWriter =
                      new StreamWriter(fullpath, true))
                  {
                      string logMessage = "";
                      if (!string.IsNullOrEmpty(stackTrace))
                      {
                          if (inOut)//IN
                          {
                              logMessage = string.Format("{0} U:{1} IN:{2} E:{3}", DateTime.Now.ToString(), userName, message, stackTrace);
                          }
                          else//OUT
                          {
                              logMessage = string.Format("{0} U:{1} OUT:{2} E:{3}", DateTime.Now.ToString(), userName, message, stackTrace);
                          }
                      }
                      else
                      {
                          if (inOut)//IN
                          {
                              logMessage = string.Format("{0} U:{1} IN:{2}", DateTime.Now.ToString(), userName, message);
                          }
                          else//OUT
                          {
                              logMessage = string.Format("{0} U:{1} OUT:{2}", DateTime.Now.ToString(), userName, message);
                          }
                      }
      
                      logWriter.WriteLine(logMessage);
      
                  }
              }
              finally
              {
                  locker.ReleaseWriterLock();
              }
          }
      
      }
      

      现在我可以像下面这样使用这个单例对象来保存日志条目

          LogMessage.Instance.SaveLogMessage(Context.User.Identity.Name, "Custom Message" + " M:" + MethodInfo.GetCurrentMethod().Name + "@" + Path.GetFileName(Page.AppRelativeVirtualPath), "", true); 
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-05-12
        • 2020-12-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-11-13
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多