Search code examples
c#mauinlog

Where do I put the NLog.config file in a MAUI project?


We are currently starting to switch vom xamarin to maui. In the first steps, I wanted to implement logging via NLog. After some starting problems, I got it running for iOS. When I try to run it on Android, I get the exception "System.IO.FileNotFoundException".

Unfortunately there is nothing on the internet - at least not that I found. Maybe some of you guys could help me.

FYI: At the moment the NLog.config file is located at the root folder of the project.

(I know in Xamarin we had to put the NLog.Config file into the assets and that there is also a assets-folder for Android in Maui. I just hoped, that we don´t have to put the same file twice in one project..)


Solution

  • So I finally figured it out. After trying several different spots where to put the config file and where to load it, I finally fount the correct spot for both.

    Luckily it is possible to put the NLog.config-file directly in the root folder of the MAUI-project. If you want to load the config file just use this statement in App() in the App.xaml.cs file. Therefor I used a method (we have a base-project which we inheritate)

    protected override void InitializeLogger()
    {
        LogManager.Setup().RegisterMauiLog().LoadConfigurationFromAssemblyResource(typeof(App).Assembly);
        _NLog = LogManager.GetCurrentClassLogger();
    }
    

    Works for Android and iOS and you don't have to specify anything in the AppDelegate.cs (iOS) or MainActivity.cs (Android) files.