admin管理员组

文章数量:1405192

I've started developing with .NET Maui. I'm still learning. One of the early things I did was to get Nlog logging to a file with an Android phone. Now I'm getting System.AggregateException: 'One or more errors occurred. (Access to the path '/storage/self/primary/Documents/xxx/Logs/xxx.log' is denied.)' This when I run the app in Visual Studio 2022 connected to the phone (which I've not done for a few weeks) with Android 13 API 33. There is no such issue when running with an emulator - for example Pixel 7 Android 15 API 35. Any ideas? Below is the nlog.config.

<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns=".xsd"
      xmlns:xsi=";
      xsi:schemaLocation=".xsd C:\Users\Ian\.nuget\packages\nlog.schema\5.3.4\contentFiles\any\any\NLog.xsd"
      autoReload="true"
      throwExceptions="true"
      throwConfigExceptions="true"
      internalLogLevel="Error"
      internalLogFile="/storage/self/primary/Documents/HubSurveyor/Logs/nlog-internal.log">

    <targets>
        <!--
            Output to emulator and phone
        -->
        <target 
            xsi:type="File" 
            name="f" 
            fileName="/storage/self/primary/Documents/HubSurveyor/Logs/HubSurveyor.log" 
            layout="${longdate} ${threadid} ${uppercase:${level}} ${callsite} ${message} ${exception:format=Message,StackTrace,Data:maxInnerExceptionLevel=10}" 
            archiveEvery="Day"
            archiveFileName="/storage/self/primary/Documents/HubSurveyor/Logs/archive/HubSurveyor-{#}.log"
            archiveNumbering="Date"
            archiveDateFormat="yyyyMMdd"
            maxArchiveFiles="21"
        />
        <target xsi:type="Debugger" name="debugger" layout="${longdate} ${threadid} ${uppercase:${level}} ${callsite} ${message} ${exception:format=Message,StackTrace,Data:maxInnerExceptionLevel=10}"  />
    </targets>

    <rules>
        <logger name="*" minlevel="Trace" writeTo="f" />
        <logger name="*" minlevel="${when:when='${environment:MAUI_ENVIRONMENT}' == 'Development':inner=Trace:else=Off}" writeTo="debugger" />
    </rules>
</nlog>

I've started developing with .NET Maui. I'm still learning. One of the early things I did was to get Nlog logging to a file with an Android phone. Now I'm getting System.AggregateException: 'One or more errors occurred. (Access to the path '/storage/self/primary/Documents/xxx/Logs/xxx.log' is denied.)' This when I run the app in Visual Studio 2022 connected to the phone (which I've not done for a few weeks) with Android 13 API 33. There is no such issue when running with an emulator - for example Pixel 7 Android 15 API 35. Any ideas? Below is the nlog.config.

<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project./schemas/NLog.xsd"
      xmlns:xsi="http://www.w3./2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.nlog-project./schemas/NLog.xsd C:\Users\Ian\.nuget\packages\nlog.schema\5.3.4\contentFiles\any\any\NLog.xsd"
      autoReload="true"
      throwExceptions="true"
      throwConfigExceptions="true"
      internalLogLevel="Error"
      internalLogFile="/storage/self/primary/Documents/HubSurveyor/Logs/nlog-internal.log">

    <targets>
        <!--
            Output to emulator and phone
        -->
        <target 
            xsi:type="File" 
            name="f" 
            fileName="/storage/self/primary/Documents/HubSurveyor/Logs/HubSurveyor.log" 
            layout="${longdate} ${threadid} ${uppercase:${level}} ${callsite} ${message} ${exception:format=Message,StackTrace,Data:maxInnerExceptionLevel=10}" 
            archiveEvery="Day"
            archiveFileName="/storage/self/primary/Documents/HubSurveyor/Logs/archive/HubSurveyor-{#}.log"
            archiveNumbering="Date"
            archiveDateFormat="yyyyMMdd"
            maxArchiveFiles="21"
        />
        <target xsi:type="Debugger" name="debugger" layout="${longdate} ${threadid} ${uppercase:${level}} ${callsite} ${message} ${exception:format=Message,StackTrace,Data:maxInnerExceptionLevel=10}"  />
    </targets>

    <rules>
        <logger name="*" minlevel="Trace" writeTo="f" />
        <logger name="*" minlevel="${when:when='${environment:MAUI_ENVIRONMENT}' == 'Development':inner=Trace:else=Off}" writeTo="debugger" />
    </rules>
</nlog>
Share Improve this question edited Mar 10 at 11:00 Ian asked Mar 9 at 9:10 IanIan 6241 gold badge5 silver badges18 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

If you saved at `/storage/self/primary/Documents` path, you can create a error in your application, then open Files app, click the hamburger button at the top-left corner, click the `Documents` folder and `/sdcard/Documents/` folder in your android 15 emulator, could you see log folder? If you cannot find this folder. You still cannot access this Documents folder.

You can try to Declare the MANAGE_EXTERNAL_STORAGE permission in the Androidmanifest.xml file

<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />

Then, use following code to judge current version and pop a Snackbar in OnCreate method of MainActivity. After grand MANAGE_EXTERNAL_STORAGE permission, you can access this `/storage/self/primary/Documents` path for android.

public class MainActivity : AppCompatActivity  
       {  
           protected override void OnCreate(Bundle savedInstanceState)  
           {  
               base.OnCreate(savedInstanceState);  
               Xamarin.Essentials.Platform.Init(this, savedInstanceState);  
               // Set our view from the "main" layout resource  
               SetContentView(Resource.Layout.activity_main);  


               if (Android.OS.Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.R)  
               {  
                   if (!Environment.IsExternalStorageManager)  
                   {  
                       Snackbar.Make(FindViewById(Android.Resource.Id.Content), "Permission needed!", Snackbar.LengthIndefinite)  
                               .SetAction("Settings", new MyClickHandler(this)).Show();  
                   }  

               }  
   }

本文标签: How to avoid 39Access to the pathdenied39 logging to file on Android with NET MauiStack Overflow