简体   繁体   English

在Xamarin(UWP)中通过TraceSource记录

[英]Logging via TraceSource in Xamarin (UWP)

I just want to log to console and to a log file, using a standard TraceSource, in my Xamarin app that will run on UWP, Mac OS X, iOS and Android. 我只想在我的Xamarin应用程序中使用标准TraceSource登录到控制台并登录到日志文件,该应用程序将在UWP,Mac OS X,iOS和Android上运行。 I'm developing/debugging on UWP. 我正在开发/调试UWP。

TraceSource, TraceListener, and TextWriterTraceListener are indeed all available in .Net Standard library, so perhaps I'm setting it up incorrectly? .Net标准库中确实提供了TraceSource,TraceListener和TextWriterTraceListener,所以也许我设置不正确? Most places on the Internet insist on setting up trace listeners in an app.config file, but this is not applicable nor possible for Xamarin apps. Internet上的大多数地方都坚持在app.config文件中设置跟踪侦听器,但这不适用于Xamarin应用程序。 So here is my logging initialization code, mostly based on an example in Microsoft docs: 所以这是我的日志记录初始化代码,主要基于Microsoft文档中的示例:

        private void SetupLogging()
        {
            Trace.Listeners.Add(new TextWriterTraceListener(Console.Out, "consoleTraceListener"));
            string logFilePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), "Application.log");
            if (!File.Exists(logFilePath)) File.Create(logFilePath);

            var logFileTraceListener = new TextWriterTraceListener(logFilePath, "logFileTraceListener");
            Trace.Listeners.Add(logFileTraceListener);

            Trace.Write("Test");
            Trace.TraceInformation("Logging Initialized. Log file location: " + logFilePath);
            Trace.Flush();
        }

When I run this in a Xamarin UWP app, a file is created but nothing is written to it, nor can I find anything in the Output of the program (there is no ConsoleTraceListener so I'm trying to write a TextWriterTraceListener to Console.Out ). 当我在Xamarin UWP应用中运行此文件时,将创建一个文件,但未写入任何文件,也无法在程序的输出中找到任何内容(没有ConsoleTraceListener因此我试图将TextWriterTraceListener写入Console.Out )。 Can someone provide a working example for Xamarin? 有人可以提供Xamarin的有效示例吗? (I haven't tried the Android or iOS apps yet; want to get UWP on the local machine working first.) (我还没有尝试过Android或iOS应用;想要先在本地计算机上运行UWP。)

The problem is that you passed wrong string parameter to TextWriterTraceListener method. 问题是您将错误的字符串参数传递给TextWriterTraceListener方法。 Please try to pass Stream parameter. 请尝试传递Stream参数。 You could use following code directly. 您可以直接使用以下代码。 by the way, you'd better use LocalApplicationData SpecialFolder that could be accessed successfully within uwp. 顺便说一句,您最好使用可以在uwp中成功访问的LocalApplicationData SpecialFolder。

private void SetupLogging()
{
    Trace.Listeners.Add(new TextWriterTraceListener(Console.Out, "consoleTraceListener"));
    string logFilePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Application.log");
    if (!File.Exists(logFilePath))
    {
        File.Create(logFilePath);
    }
    var logFileTraceListener = new TextWriterTraceListener(File.Open(logFilePath,FileMode.Open), "logFileTraceListener");
    Trace.Listeners.Add(logFileTraceListener);

    Trace.Write("Test");
    Trace.TraceInformation("Logging Initialized. Log file location: " + logFilePath);
    Trace.Flush();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM