微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

WebService写系统日志

 

 

     string sourcename = "ycsmart";
        EventLogEntryType errotype = EventLogEntryType.SuccessAudit;
        try
        {
            EventLog mylog = new EventLog();
            if (!EventLog.sourceExists(sourcename))
            {
                EventLog.CreateEventSource(sourcename,sourcename);
            }

            mylog.source = sourcename;

            mylog.MaximumKilobytes = 1024 * 20;

            mylog.ModifyOverflowPolicy(OverflowAction.OverwriteAsNeeded,30);

            mylog.WriteEntry(sMsg,errotype);
        }
        catch (Exception ex)
        {
            //MessageBox.Show(ex.Message);
        }

 

ASP.Net认情况下,是没有写系统日志的权限的。要在ASP.Net中写系统日志,首先我们要先有其权限。我们可以通过设置或修改注册表的权限来解决

方法1:“开始->运行”,输入命令,“regedt32”,找到“System->CurrentControlSet->Services->Eventlog”,选择“安全->权限->添加”,然后找到本机的“AspNet”用户,加进来并且给读取权限就好了,加进来后目录中会多一个“aspnet_wp account”

方法2:在注册表:HKEY_LOCAL_MACHINE\SYstem\CurrentControlSet\Service\EventLog增加User的完全操纵权限

using System;
using System.Diagnostics;
using System.Text;

namespace CorePlus.Framework.Utility
{
 /// <summary>
 /// 写日志的CLASS
 /// </summary>
 public class LogUtility
 {
  public enum EVENT
  {
   /// <summary>重大错误</summary>
   EVENT_ERROR,
   /// <summary>操作失败</summary>
   EVENT_FAILUREAUDIT,
   /// <summary>一般信息</summary>
   EVENT_@R_794_4045@ION,
   /// <summary>有效的,成功的操作</summary>
   EVENT_SUCCESSAUDIT,
   /// <summary>警告</summary>
   EVENT_WARNING,
  }

  private static TextWriterTraceListener listner = null;

  /// <summary>
  /// 构造函数
  /// </summary>
  public LogUtility () {}

  /// <param name="loginID">登录ID</param>
  /// <param name="screenID">画面ID</param>
  /// <param name="level">警告级别</param>
  /// <param name="message">日志信息</param>
  public static void Logging (
   string loginID,
   string screenID,
   EVENT level,
   string message )
  {
   // 变量定义
   StringBuilder  outputMessage = null; // 输出的信息
   string    targetLevelString = null;
   EventLogEntryType Eventtype = new EventLogEntryType();

   switch (level)
   {
    case EVENT.EVENT_ERROR:
     Eventtype = EventLogEntryType.Error;
     break;

    case EVENT.EVENT_FAILUREAUDIT:
     Eventtype = EventLogEntryType.FailureAudit;
     break;

    case EVENT.EVENT_@R_794_4045@ION:
     Eventtype = EventLogEntryType.@R_794_4045@ion;
     break;

    case EVENT.EVENT_SUCCESSAUDIT:
     Eventtype = EventLogEntryType.SuccessAudit;
     break;

    case EVENT.EVENT_WARNING:
     Eventtype = EventLogEntryType.Warning;
     break;
   }

   // 日志信息的拼合
   outputMessage = MakeMessage( loginID,screenID,targetLevelString,message );

   // 写日志
   Logging( Eventtype,outputMessage.ToString() );
  }

  /// <summary>
  /// 向日志管理器写日志
  /// </summary>
  /// <param name="level">错误级别</param>
  /// <param name="message">输出的信息</param>
  private static void Logging ( EventLogEntryType level,string message )
  {
   // 变量定义
   string  logName  = null; // 日志名
   string  machineName = null; // 机器名
   string  sourceName = null; // SourceName
   EventLog eventLog = null; // EventLog

   logName  = "Eventlog2";  // 日志名
   machineName = "."; // 机器名
   sourceName = "Eventlog2"; // SourceName

   eventLog = new EventLog( logName,machineName,sourceName );
   eventLog.WriteEntry( message,level );
   eventLog = null;
  }

  /// <summary>
  /// 日志最终拼合
  /// </summary>
  /// <param name="loginID">登录ID</param>
  /// <param name="screenID">画面ID</param>
  /// <param name="levelString">错误级别</param>
  /// <param name="message">日志信息</param>
  /// <returns>输出的日志信息</returns>
  private static StringBuilder MakeMessage (
   string loginID,
   string levelString,
   string message )
  {
   // 变量定义
   StringBuilder retMessage = new StringBuilder();

   retMessage.Append( "【登录ID】" );
   retMessage.Append( loginID );
   retMessage.Append( "\n【画面ID】" );
   retMessage.Append( screenID );
   retMessage.Append( "\n【错误级别】" );
   retMessage.Append( levelString );
   retMessage.Append( "\n【错误信息】\n" );
   retMessage.Append( message );

   return retMessage;
    }
  }
}

// 在FORM中输出日志
  private void button1_Click(object sender,System.EventArgs e)
  {
   LogUtility.Logging( "bbbbbb","22222222",LogUtility.EVENT.EVENT_ERROR,"3333333" );
  }

运行完程序后打开日志管理器看看~~~~~~~~~怎么样?不错吧,呵呵.

一个方法

using System; using System.Collections.Generic; using System.Text; using System.Diagnostics; namespace Log {     class LogWirter     {         /// <summary>         /// 事件源名称         /// </summary>         private string eventSourceName;         EventLogEntryType eventLogType;         public LogWirter()         {             eventSourceName = "test";             eventLogType = EventLogEntryType.Error;         }         /// <summary>         /// 消息事件源名称         /// </summary>         public string EventSourceName         {             set { eventSourceName = value; }         }         /// <summary>         /// 消息事件类型         /// </summary>         public EventLogEntryType EventLogType         {             set { eventLogType = value; }         }         /// <summary>         /// 写入系统日志         /// </summary>         /// <param name="message">事件内容</param>         public void LogEvent(string message)         {             if (!EventLog.sourceExists(eventSourceName))             {                 EventLog.CreateEventSource(eventSourceName, "Application");             }             EventLog.WriteEntry(eventSourceName, message, EventLogEntryType.Error);         }     } }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐