通过Xamarin(UWP)中的TraceSource进行日志记录

通过Xamarin(UWP)中的TraceSource进行日志记录,xamarin,uwp,tracelistener,Xamarin,Uwp,Tracelistener,我只想在我的Xamarin应用程序中使用标准TraceSource登录控制台和日志文件,该应用程序将在UWP、Mac OS X、iOS和Android上运行。我正在UWP上开发/调试 TraceSource、TraceListener和TextWriterTraceListener确实都可以在.Net标准库中使用,所以我可能设置不正确?互联网上的大多数地方都坚持在app.config文件中设置跟踪侦听器,但这不适用于Xamarin应用程序,也不可能。下面是我的日志初始化代码,主要基于Micros

我只想在我的Xamarin应用程序中使用标准TraceSource登录控制台和日志文件,该应用程序将在UWP、Mac OS X、iOS和Android上运行。我正在UWP上开发/调试

TraceSource、TraceListener和TextWriterTraceListener确实都可以在.Net标准库中使用,所以我可能设置不正确?互联网上的大多数地方都坚持在app.config文件中设置跟踪侦听器,但这不适用于Xamarin应用程序,也不可能。下面是我的日志初始化代码,主要基于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();
        }

当我在Xamarin UWP应用程序中运行此程序时,会创建一个文件,但没有写入任何内容,在程序的输出中也找不到任何内容(没有
ConsoleTraceListener
,因此我尝试将
TextWriterTraceListener
写入
Console.Out
)。有人能为Xamarin提供一个工作示例吗?(我还没有尝试Android或iOS应用程序;想先让本地机器上的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();
}

问题是您向方法传递了错误的字符串参数。请尝试传递
参数。您可以直接使用以下代码。顺便说一下,您最好使用可以在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();
}

下面的答案有用吗?当然可以,谢谢!下面的答案有用吗?当然可以,谢谢!在我的android应用程序中,我想听听第三方软件包发出的TraceSource事件,我已经在链接中详细说明了我的问题。您能否检查并帮助我如何在日志文件中写入TraceSource事件?我恐怕无法回答第三方库案例,更多社区将查看您的问题,请添加xamarin anroid标签。在我的android应用程序中,我想听听第三方软件包发出的TraceSource事件,我在链接中详细说明了我的问题。你能检查并帮助我如何在日志文件中写入TraceSource事件吗?我恐怕无法回答第三方库案例,更多社区将查看你的问题,请添加xamarin anroid标签。