Гость
Целевая тема:
Создать новую тему:
Автор:
Форумы / WinForms, .Net Framework [игнор отключен] [закрыт для гостей] / MyEventLogClass / 4 сообщений из 4, страница 1 из 1
17.09.2013, 13:03
    #38398747
wiktor591
Участник
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
MyEventLogClass
Добрый день, коллеги.

Не кто не подскажет, есть уже написанный класс для MyEventLogClass не для консоли.

Спасибо!
...
Рейтинг: 0 / 0
17.09.2013, 16:17
    #38399077
wiktor591
Участник
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
MyEventLogClass
Вот кому интересно, просто класс.

Но тут много еще параметров не хватает. ( но их не сложно добавить )


Код: c#
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
using System.Text.RegularExpressions;
using System.Windows.Forms;

public static class MyEventLogClass
{
    private static EventLog evLog;

    private static string EventLogName = Application.ProductName;

    private static bool CheckSourceExists(string source)
    {
        if (EventLog.SourceExists(source))
        {
            evLog = new EventLog { Source = source };
            if (evLog.Log != EventLogName)
            {
                EventLog.DeleteEventSource(source);
            }
        }

        if (!EventLog.SourceExists(source))
        {
            EventLog.CreateEventSource(source, EventLogName);
            EventLog.WriteEntry(source, String.Format("Event Log Created '{0}'/'{1}'", EventLogName, source), EventLogEntryType.Information);
        }

        
        
        return EventLog.SourceExists(source);
    }

    public static void WriteEventToMyLog(string text, EventLogEntryType type)
    {
        string source = Application.ProductName;

        if (CheckSourceExists(source))
        {
            EventLog.WriteEntry(source, text, type);
        }
    }
}
...
Рейтинг: 0 / 0
17.09.2013, 21:22
    #38399378
Твой Друх
Участник
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
MyEventLogClass
Есть еще решение, хоть и монструозненькое:
Код: c#
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
50.
51.
52.
53.
54.
55.
56.
57.
58.
59.
60.
61.
62.
63.
64.
65.
66.
67.
68.
69.
70.
71.
72.
73.
74.
75.
76.
77.
78.
79.
80.
81.
82.
83.
84.
85.
86.
87.
88.
89.
90.
91.
92.
93.
94.
95.
96.
97.
98.
99.
Логирование с использованием NLog + Sentinel 
Nlog (Nuget) - 
PM> Install-Package NLog

Pasted from <http://www.nuget.org/packages/nlog> 


Sentinel - (http://sentinel.codeplex.com/downloads/get/clickOnce/Sentinel.application)



//1) Класс логгера
using NLog;
using NLog.Config;
using NLog.Targets;

//USAGE SAMPLE:
//private void button1_Click(object sender, EventArgs e)
//{
            
//    try
//    {
//        Log.Instance.Debug("We're going to throw an exception now.");
//        Log.Instance.Warn("It's gonna happen!!");
//        throw new ApplicationException();
//    }
//    catch (ApplicationException ae)
//    {
//        Log.Instance.ErrorException("Error doing something...", ae);
//    }
            
//}

internal static class Log
{
    public static Logger Instance { get; private set; }

    static Log()
    {
#if DEBUG
        // Setup the logging view for Sentinel - http://sentinel.codeplex.com
        var sentinalTarget = new NLogViewerTarget()
                             {
                                 Name = "sentinal",
                                 Address = "udp://127.0.0.1:9999",
                                 IncludeNLogData = false
                             };
        var sentinalRule = new LoggingRule("*", LogLevel.Trace, sentinalTarget);
        LogManager.Configuration.AddTarget("sentinal", sentinalTarget);
        LogManager.Configuration.LoggingRules.Add(sentinalRule);

        // Setup the logging view for Harvester - http://harvester.codeplex.com
        var harvesterTarget = new OutputDebugStringTarget()
                              {
                                  Name = "harvester",
                                  Layout = "${log4jxmlevent:includeNLogData=false}"
                              };
        var harvesterRule = new LoggingRule("*", LogLevel.Trace, harvesterTarget);
        LogManager.Configuration.AddTarget("harvester", harvesterTarget);
        LogManager.Configuration.LoggingRules.Add(harvesterRule);
#endif

        LogManager.ReconfigExistingLoggers();

        Instance = LogManager.GetCurrentClassLogger();
    }
}

//2) Файл NLog.config (В свойствах установить Copy To Output Directory => Copy Always)
<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      autoReload="true"
      throwExceptions="false">

  <variable name="appName" value="YourAppName" />

  <targets async="true">
    <target xsi:type="File"
            name="default"
            layout="${longdate} - ${level:uppercase=true}: ${message}${onexception:${newline}EXCEPTION\: ${exception:format=ToString}}"
            fileName="${specialfolder:ApplicationData}\${appName}\Debug.log"
            keepFileOpen="false"
            archiveFileName="${specialfolder:ApplicationData}\${appName}\Debug_${shortdate}.{##}.log"
            archiveNumbering="Sequence"
            archiveEvery="Day"
            maxArchiveFiles="30"
    />

    <target xsi:type="EventLog"
            name="eventlog"
            source="${appName}"
            layout="${message}${newline}${exception:format=ToString}"/>
  </targets>
  <rules>
    <logger name="*" writeTo="default" minlevel="Info" />
    <logger name="*" writeTo="eventlog" minlevel="Error" />
  </rules>
</nlog>
...
Рейтинг: 0 / 0
18.09.2013, 10:19
    #38399694
wiktor591
Участник
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
MyEventLogClass
Твой Друх,

Спасибо
...
Рейтинг: 0 / 0
Форумы / WinForms, .Net Framework [игнор отключен] [закрыт для гостей] / MyEventLogClass / 4 сообщений из 4, страница 1 из 1
Найденые пользователи ...
Разблокировать пользователей ...
Читали форум (0):
Пользователи онлайн (0):
x
x
Закрыть


Просмотр
0 / 0
Close
Debug Console [Select Text]