كيفية القيام بما يلي: استخدام TraceSource و عوامل تصفية رسائل التتبع

إحدى الميزات الجديدة في الإصدار 2.0 من برنامج.NET Framework هو على النظام تتبع محسن. premهوe أساسى هو بدون تغيير: تتبع رسائل يتم إرسالها من خلال رموز التبديل لرسائل التقرير بيانات إلى وسيطة إخراج مقترن. فرق أساسي للإصدار 2.0 هو بدء تتبعات عبر مثيلات TraceSourceفئة. TraceSource is intended to function as an enhanced tracing system and can be used in place of the static methods of the older Trace and Debug tracing classes. دراية Traceو Debugفئات لا يزال exهوt، ولكن الممارسة الموصى بها هو لاستخدامها TraceSourceفئة للتتبع.

يصف هذا الموضوع استخدم TraceSourceبدقة مع على ملف تكوين تطبيق. هو المحتملة، على الرغم من أنه من غير المستحسن، للتتبع باستخدام TraceSourceبدون استخدام ملف تكوين. للحصول تشغيل المعلومات حول تتبع بدون ملف تكوين، راجع كيفية القيام بما يلي: قم بإنشاء و يهيّئ المصادر تتبع.

إلى إنشاء ويهيّئ مصدر التتبع الخاص بك

  • الخطوة الأولى ل instrumenting أحد تطبيقات باستخدام تتبع هو لإنشاء مصدر بيانات تتبع. يمكنك إنشاء عملية تتبع منفصلة في مشاريع الكبيرة ذات المكونات المتنوعة، المصدر لكل مكون. ممارسة الموصى بها إلى باستخدام اسم تطبيق عن اسم مصدر التتبع. هذا سوف تسهيل إلى الاحتفاظ تتبعات مختلفة بشكل منفصل. التعليمة البرمجية التالية بإنشاء مصدر جديد تتبع ( mySource)و استدعاء أسلوب ( Activity1) الذي يتتبع أحداث. يتم كتابة الرسائل تتبع بواسطة وحدة إصغاء التتبع الافتراضية.

    using System;
    using System.Diagnostics;
    using System.Threading;
    
    namespace TraceSourceApp
    {
        class Program
        {
            private static TraceSource mySource = 
                new TraceSource("TraceSourceApp");
            static void Main(string[] args)
            {
                Activity1();
                mySource.Close();
                return;
            }
            static void Activity1()
            {
                mySource.TraceEvent(TraceEventType.Error, 1, 
                    "Error message.");
                mySource.TraceEvent(TraceEventType.Warning, 2, 
                    "Warning message.");
            }
        }
    }
    

لإنشاء وتهيئة رسائل التتبع وعوامل التصفية

  • تعليمات برمجية في إجراء أول لم يؤد إلى برمجياً تعريف أية رسائل التتبع أو عوامل التصفية. The تعليمات برمجية alone نتائج في the trace رسائل being written إلى the الافتراضي trace listener. إلى configure specific trace listeners و their associated filters, تحرير the ملف تكوين that corresponds إلى the اسم of your تطبيق. في this ملف, you can إضافة أو إزالة a listener, التعيين the خصائص و عامل تصفية for a listener, أو إزالة listeners. The following ملف تكوين مثال shows how إلى يهيّئ a console trace listener و a كاتب نص trace listener for the trace المصدر that هو تاريخ الإنشاء في the preceding إجراء. في addition إلى configuring the trace listeners, the ملف تكوين creates filters for كلاهما of the listeners و creates a المصدر تبديل for the trace المصدر. الثاني techniques are shown for إضافة trace listeners: إضافة the listener directly إلى the trace المصدر و إضافة a listener إلى the shared listeners مجموعة و then إضافة it حسب الاسم إلى the trace المصدر. The filters identified for the الثاني listeners are initialized مع different المصدر levels. This نتائج في some رسائل being written بواسطة فقط واحد of the الثاني listeners.

    <configuration>
      <system.diagnostics>
        <sources>
          <source name="TraceSourceApp" 
            switchName="sourceSwitch" 
            switchType="System.Diagnostics.SourceSwitch">
            <listeners>
              <add name="console" 
                type="System.Diagnostics.ConsoleTraceListener">
                <filter type="System.Diagnostics.EventTypeFilter" 
                  initializeData="Warning"/>
              </add>
              <add name="myListener"/>
              <remove name="Default"/>
            </listeners>
          </source>
        </sources>
        <switches>
          <add name="sourceSwitch" value="Warning"/>
        </switches>
        <sharedListeners>
          <add name="myListener" 
            type="System.Diagnostics.TextWriterTraceListener" 
            initializeData="myListener.log">
            <filter type="System.Diagnostics.EventTypeFilter" 
              initializeData="Error"/>
          </add>
        </sharedListeners>
      </system.diagnostics>
    </configuration>
    

إلى تغيير the المستوى at which a listener writes a trace رسالة

  • The ملف تكوين initializes the إعدادات for the trace المصدر at the الوقت the تطبيق هو initialized. إلى تغيير those إعدادات you must تغيير the ملف تكوين و إعادة التشغيل the تطبيق أو programmatically تحديث the تطبيق using the Trace.Refresh أسلوب. The تطبيق can dynamically تغيير the خصائص التعيين بواسطة the ملف تكوين إلى يمنع أي إعدادات specified بواسطة the مستخدم. For مثال, you might want إلى assure that حرج رسائل are دوماً sent إلى a ملف نصي, regardless of the الحالي تكوين إعدادات.

    using System;
    using System.Diagnostics;
    using System.Threading;
    
    namespace TraceSourceApp
    {
        class Program
        {
            private static TraceSource mySource = 
                new TraceSource("TraceSourceApp");
            static void Main(string[] args)
            {
                Activity1();
    
                // Change the event type for which tracing occurs.
                // The console trace listener must be specified 
                // in the configuration file. First, save the original
                // settings from the configuration file.
                EventTypeFilter configFilter = 
                    (EventTypeFilter)mySource.Listeners["console"].Filter;
    
                // Then create a new event type filter that ensures 
                // critical messages will be written.
                mySource.Listeners["console"].Filter =
                    new EventTypeFilter(SourceLevels.Critical);
                Activity2();
    
                // Allow the trace source to send messages to listeners 
                // for all event types. This statement will override 
                // any settings in the configuration file.
                mySource.Switch.Level = SourceLevels.All;
    
                // Restore the original filter settings.
                mySource.Listeners["console"].Filter = configFilter;
                Activity3();
                mySource.Close();
                return;
            }
            static void Activity1()
            {
                mySource.TraceEvent(TraceEventType.Error, 1, 
                    "Error message.");
                mySource.TraceEvent(TraceEventType.Warning, 2, 
                    "Warning message.");
            }
            static void Activity2()
            {
                mySource.TraceEvent(TraceEventType.Critical, 3, 
                    "Critical message.");
                mySource.TraceInformation("Informational message.");
            }
            static void Activity3()
            {
                mySource.TraceEvent(TraceEventType.Error, 4, 
                    "Error message.");
                mySource.TraceInformation("Informational message.");
            }
        }
    }
    

راجع أيضًا:

المهام

كيفية القيام بما يلي: قم بإنشاء و يهيّئ المصادر تتبع

المرجع

TraceSource

TextWriterTraceListener

ConsoleTraceListener

EventTypeFilter

المبادئ

تتبع رسائل