سؤال

الآن بعد أن تم شحن Silverlight 2 أخيرًا.أنا أتساءل عما إذا كان أي شخص قد قام بتجميع أي أطر تسجيل لذلك، ربما شيء من هذا القبيل تسجيل مكتبة المؤسسة أو log4net؟أنا مهتم بشيء يمكنه إجراء التتبع من جانب العميل وكذلك تسجيل الرسائل إلى الخادم.

حتى الآن المشروع الوحيد الذي وجدته هو تسد على CodeProject.وقد استخدم أي شخص هذا؟ما هي أفكارك حول هذا الموضوع؟

هل كانت مفيدة؟

المحلول

أنا على وشك الخوض في شيء مماثل بنفسي بالنسبة لمنتج كتبناه.أفكر في استخدام PostSharp for Silverlight لإضافة التسجيل من جانب العميل كجانب.

لقد استخدمت مشروع NLog بنجاح كبير من قبل ضمن إطار .NET Framework الكامل وإطار العمل المضغوط، لذلك على الأرجح سأأخذ كود إطار العمل الحالي وأضيف بعض أهداف التسجيل:

  • هدف System.Diagnostics قياسي لتمكين الالتقاط باستخدام DebugView، وما إلى ذلك.
  • هدف خدمة ويب غير متزامن مشابه لذلك الموجود في NLog.
  • هدف تخزين معزول مع نقل مؤجل إلى دلالات الخادم.

لقد ألقيت نظرة سريعة على Clog ويبدو أنه يعاني من عيب رئيسي واحد - فهو لا يمكنه تسجيل فشل الاتصال.لذا، بافتراض أن خادم الويب الخاص بك متصل بالإنترنت طوال الوقت، نعم سيعمل، ولكن عند حدوث مشكلات في المنبع أو على الخادم نفسه، يتم فقدان جميع بيانات التسجيل وقد يؤدي ذلك إلى تعطل تطبيقك.

نصائح أخرى

إذا كنت على استعداد لاتخاذ خوذة رائد الفضاء الخاص بك قبالة لمدة دقيقة، وفيما يلي ومسجل خفيفة الوزن لقد كتبت عن سيلفرلايت، على العميل تسجيل الدخول (للاستخدام بشكل رئيسي مع عمليات WCF ولكن يمكن أن يكون عن أي أخطاء).

وكان يستخدم أصلا في Monotouch لتطبيقات آي فون، وتم تكييفها لIsolateStorage. يمكنك استخدام الأسلوب Read لعرضه في مربع نص إذا لزم الأمر. اختبار في SL4.

/// <summary>
/// A lightweight logging class for Silverlight.
/// </summary>
public class Log
{
    /// <summary>
    /// The log file to write to. Defaults to "dd-mm-yyyy.log" e.g. "13-01-2010.log"
    /// </summary>
    public static string LogFilename { get; set; }

    /// <summary>
    /// Whether to appendthe calling method to the start of the log line.
    /// </summary>
    public static bool UseStackFrame { get; set; }

    static Log()
    {
        LogFilename = string.Format("{0}.log", DateTime.Today.ToString("dd-MM-yyyy"));
        UseStackFrame = false;
    }

    /// <summary>
    /// Reads the entire log file, or returns an empty string if it doesn't exist yet.
    /// </summary>
    /// <returns></returns>
    public static string ReadLog()
    {
        string result = "";
        IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForSite();

        if (storage.FileExists(LogFilename))
        {
            try
            {
                using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream(LogFilename,FileMode.OpenOrCreate,storage))
                {
                    using (StreamReader reader = new StreamReader(stream))
                    {
                        result = reader.ReadToEnd();
                    }
                }
            }
            catch (IOException)
            {
                // Ignore
            }
        }

        return result;
    }

    /// <summary>
    /// Writes information (not errors) to the log file.
    /// </summary>
    /// <param name="format">A format string</param>
    /// <param name="args">Any arguments for the format string.</param>
    public static void Info(string format, params object[] args)
    {
        WriteLine(LoggingLevel.Info, format, args);
    }

    /// <summary>
    /// Writes a warning (non critical error) to the log file
    /// </summary>
    /// <param name="format">A format string</param>
    /// <param name="args">Any arguments for the format string.</param>
    public static void Warn(string format, params object[] args)
    {
        WriteLine(LoggingLevel.Warn, format, args);
    }

    /// <summary>
    /// Writes a critical or fatal error to the log file.
    /// </summary>
    /// <param name="format">A format string</param>
    /// <param name="args">Any arguments for the format string.</param>
    public static void Fatal(string format, params object[] args)
    {
        WriteLine(LoggingLevel.Fatal, format, args);
    }

    /// <summary>
    /// Writes the args to the default logging output using the format provided.
    /// </summary>
    public static void WriteLine(LoggingLevel level, string format, params object[] args)
    {
        string message = string.Format(format, args);

        // Optionally show the calling method
        if (UseStackFrame)
        {
            var name = new StackFrame(2, false).GetMethod().Name;

            string prefix = string.Format("[{0} - {1}] ", level, name);
            message = string.Format(prefix + format, args);
        }

        Debug.WriteLine(message);
        WriteToFile(message);
    }

    /// <summary>
    /// Writes a line to the current log file.
    /// </summary>
    /// <param name="message"></param>
    private static void WriteToFile(string message)
    {
        try
        {
            IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForSite();
            bool b = storage.FileExists(LogFilename);

            using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream(LogFilename,FileMode.Append,storage))
            {
                using (StreamWriter writer = new StreamWriter(stream))
                {
                    writer.WriteLine("[{0}] {1}", DateTime.UtcNow.ToString(), message);
                }
            }
        }
        catch (IOException)
        {
            // throw new Catch22Exception();
        }
    }
}

/// <summary>
/// The type of error to log.
/// </summary>
public enum LoggingLevel
{
    /// <summary>
    /// A message containing information only.
    /// </summary>
    Info,
    /// <summary>
    /// A non-critical warning error message.
    /// </summary>
    Warn,
    /// <summary>
    /// A fatal error message.
    /// </summary>
    Fatal
}

وإذا كنت ترغب فقط في الرسائل إخراج التصحيح إلى وحدة التحكم. هل يمكن استخدام آلية console.log للمتصفح. I مشفرة طريقة تمديد لذلك. يمكنك العثور على بلدي بلوق .

    // http://kodierer.blogspot.com.es/2009/05/silverlight-logging-extension-method.html
    public static string Log(string message)
    {
        var msgLog = "";
        try
        {

            HtmlWindow window = HtmlPage.Window;

            //only log if a console is available
            var isConsoleAvailable = (bool)window.Eval("typeof(console) != 'undefined' && typeof(console.log) != 'undefined'");

            if (!isConsoleAvailable) return "isConsoleAvailable " + isConsoleAvailable;

            var createLogFunction = (bool)window.Eval("typeof(ssplog) == 'undefined'");
            if (createLogFunction)
            {
                // Load the logging function into global scope:
                string logFunction = @"function ssplog(msg) { console.log(msg); }";
                string code = string.Format(@"if(window.execScript) {{ window.execScript('{0}'); }} else {{ eval.call(null, '{0}'); }}", logFunction);
                window.Eval(code);
            }

            // Prepare the message
            DateTime dateTime = DateTime.Now;
            string output = string.Format("{0} - {1} - {2}", dateTime.ToString("u"), "DEBUG", message);

            // Invoke the logging function:
            var logger = window.Eval("ssplog") as ScriptObject;
            logger.InvokeSelf(output);
        }
        catch (Exception ex)
        {
            msgLog = "Error Log " + ex.Message;
        }
        return msgLog;

    }

ويمكنك استخدام هذا واحد أيضا: http://silverlightlogging.codeplex.com/

ولقد انتهى كتابة إطار تسجيل جديد من نقطة الصفر الذي يعالج هذا الخلل. أنا خلقت قائمة الانتظار المحلية التي سوف تحصل على رسائل سجل / التتبع ومن ثم القيام الترشيح وإرسالها إلى الملقم. قائمة الانتظار ثم سوف تكون مدعومة التخزين معزولة حتى لو يذهب العميل خارج الخط بشكل دائم لتلك الدورة سيتم إرسال رسائل عندما عاد عبر الإنترنت.

وأنا باستخدام ويندوز جافا سكريبت وجعلها النصية في Silverlight. ل "إنتاج"، ويمكن أن تتحول هذه النافذة خارج ولكن لا يزال حفظ خطوط سجل في الذاكرة، ثم إذا كان هناك شيء يذهب على نحو خاطئ، وإرسال التي قبالة إلى الملقم. وبهذه الطريقة أحصل على الأفضل من كلا العالمين - بسيط، في الوقت الحقيقي تسجيل على العميل لتصحيح الأخطاء وسجلات لحالات ما بعد الوفاة النائية التي قد تواجه المستخدمين

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top