• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C#如何调用EventLog

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

工作原理:

      1.1 source存在

       在写eventlog的时候,首先去找source,如果找到的话,就往这个source所在的log里面写日志。

   EventLog eventLog = new EventLog();
            eventLog.Source = $@"LisaEventLog 2018-04-17 18:37:16.907 +08:00";
            var message =
                $"{AppDomain.CurrentDomain.BaseDirectory}{Environment.NewLine}{AppDomain.CurrentDomain.FriendlyName} {DateTimeOffset.Now}";
            eventLog.WriteEntry(message, EventLogEntryType.Error);
            Console.WriteLine($@"{eventLog.Log},{eventLog.Source}");

 

      1.2 source 不存在 (直接绑定Application作为logname,然后自动创建一个source)

     https://github.com/dotnet/corefx/

 

    dotnet\corefx\src\System.Diagnostics.EventLog\src\System\Diagnostics\EventLogInternal.cs

    private void VerifyAndCreateSource(string sourceName, string currentMachineName)

     如果log没有指定,默认会使用Application

if (GetLogName(currentMachineName) == null)
this.logName = "Application";

然后自动创建一个event source

  EventLog.CreateEventSource(new EventSourceCreationData(sourceName, GetLogName(currentMachineName), currentMachineName));

  EventLog eventLog = new EventLog();
            eventLog.Source = $@"{nameof(LisaEventLog)} {DateTimeOffset.Now:yyyy-MM-dd HH:mm:ss.fff zzz}";
            var message =
                $"{AppDomain.CurrentDomain.BaseDirectory}{Environment.NewLine}{AppDomain.CurrentDomain.FriendlyName} {DateTimeOffset.Now}";
            eventLog.WriteEntry(message, EventLogEntryType.Error);
            Console.WriteLine($@"{eventLog.Log},{eventLog.Source}");

 

 

      2.指定logname和source

      2.1 source不存在

            2.1.1 logname也不存在

                    那么会自动创建log和source,然后写log

            2.1.2 logname存在

                   那么会在log下自动创建source,然后写log

      2.2 source存在

            那么这个source肯定有对应的log了,要么不指定log,让系统自动去匹配。上面的1.1

            如果要指定log的话,那么必须指定为正确的,否则会抛出异常

     3. source没有指定

   这个是不允许的

System.ArgumentException : Source property was not set before writing to the event log.
at System.Diagnostics.EventLogInternal.WriteEntry(String message, EventLogEntryType type, Int32 eventID, Int16 category, Byte[] rawData)
at System.Diagnostics.EventLog.WriteEntry(String message, EventLogEntryType type)

 

 

 

 

 public class LisaEventLog
    {
        private readonly string _logName = @"Lisa";

        public string LogName => _logName;

        public LisaEventLog()
        {
        }

        public LisaEventLog(string logName)
        {
            _logName = logName;
        }

        public void WriteEntry(string error, EventLogEntryType type)
        {
            var sourceName = AppDomain.CurrentDomain.FriendlyName;
            if (!EventLog.SourceExists(sourceName))
            {
                EventLog.CreateEventSource(sourceName, _logName);
            }
            using (EventLog eventLog = new EventLog(_logName))
            {
                eventLog.Source = sourceName;
                var message = $"{AppDomain.CurrentDomain.BaseDirectory}{Environment.NewLine}{error}";
                eventLog.WriteEntry(message, type);
            }
        }
    }

 

左侧栏里面的叫做LogName,每一条event log中的source列,对应的是source

 

EventLog.Entries

 这里的entries是指event log,比如上图中对应有5个。

 

 

System.ArgumentException : Only the first eight characters of a custom log name are significant, and there is already another log on the system using the first eight characters of the name given. Name given: 'Application1', name of existing log: 'Application'.
at System.Diagnostics.EventLog.CreateEventSource(EventSourceCreationData sourceData)
at System.Diagnostics.EventLogInternal.VerifyAndCreateSource(String sourceName, String currentMachineName)
at System.Diagnostics.EventLogInternal.WriteEntry(String message, EventLogEntryType type, Int32 eventID, Int16 category, Byte[] rawData)
at System.Diagnostics.EventLog.WriteEntry(String message, EventLogEntryType type)

 

 

System.ArgumentException : The source 'klnagent2' is not registered in log 'Application'. (It is registered in log 'Appplicat'.) " The Source and Log properties must be matched, or you may set Log to the empty string, and it will automatically be matched to the Source property.
at System.Diagnostics.EventLogInternal.VerifyAndCreateSource(String sourceName, String currentMachineName)
at System.Diagnostics.EventLogInternal.WriteEntry(String message, EventLogEntryType type, Int32 eventID, Int16 category, Byte[] rawData)
at System.Diagnostics.EventLog.WriteEntry(String message, EventLogEntryType type)
at ExcelTest.Test.TestEventLog() in D:\ChuckLu\Git\Edenred\LISA_5.0.0.0\ExcelTest\Test.cs:line 692


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C++用new创建对象和不用new创建对象的区别解析发布时间:2022-07-13
下一篇:
golang调用c++文件golang的cgo支持调用C++的方法发布时间:2022-07-13
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap