tafuji / Xamarin-Forms-NLog-Sample

Simple Xamarin.Forms logging sample with NLog
30 stars 14 forks source link

File logging doesn't create file #1

Open mtbgithub opened 6 years ago

mtbgithub commented 6 years ago

I tried this project on a Galaxy S9 and a Mate 10 (both Oreo) and although it created the path "internal storage/Android/data/NLogSample.Droid/files" no file appeared in the target folder. Any idea what gives? I've tried NLog in my own app and on neither droid not iphone does it create the file.

tafuji commented 6 years ago

@mtbgithub

NLog's auto-loading NLog.config feature in Xamarin might be broken (see the last comment in https://github.com/NLog/NLog/issues/1158).

Could you try to load NLog.config file manually? (see sample code) I rewrote my sample codes to fix this issue and my sample codes worked on both Android and iPhone.

I pushed the fixed codes in "develop-netsdandard" branch. I hope it would help you.

public class LoggingServiceImplementation : ILoggingService
{
    private ILogger _logger;
    private ILogger Logger
    {
        get
        {
            if(_logger == null)
            {
                var configName = string.Empty;
                #if __ANDROID__
                configName = "assets/NLog.config";
                #endif
                #if __IOS__
                configName = "NLog.config";
                #endif
                LogManager.Configuration = new XmlLoggingConfiguration(configName);
                _logger = LogManager.GetCurrentClassLogger();
            }
            return _logger;
        }
    }
    public void Error(string message)
    {
        Logger.Error(message);
    }
    // continue...
}
mtbgithub commented 6 years ago

Ok, I am clearly missing something very fundamental here but I still cannot get this to work. I appreciate your help with this, didn't matter what examples I followed I just couldn't get this to work, it's like NLog just doesn't like me!!

Your example compiles fine (on Android) and runs o a bunch of devices (rooted and pure) and still I get no file. Interestingly, when I try to build it for iOS I get the following error message...

"Compiling IB documents for earlier than iOS 7 is no longer supported. NLogSample.iOS"

I'm using VS 2017 on Win 8.1.

It might sound a stupid question but what is the folder path to the folder containing the log on your Android device please?

tafuji commented 6 years ago

@mtbgithub

Firstly, the path in my Android device is "/data/data/NLogSample.Android/logs". The path is defined in fileName attribute of target element in NLog.config file.

Generally, the path should be "/data/data/[package name of your Android application]/[your folder]".

Here is my sample NLog.config file.

<target name="logfile"
    xsi:type="File"
    fileName="/data/data/NLogSample.Android/logs/nlog-sample.csv"
    ...
    >
    ....
</target>

Here is the screenshot of the path in my Android device (Nexus 5).

logfolder

Secondly, your build failed with error because your build environment didn't support building iOS 7 applicaiton. Could you change the value of deployment target in your Info.plist file?

Shashidhar014 commented 5 years ago

Hi I can See the log cats in console but how to get that log file?

tafuji commented 5 years ago

@Shashidhar014

If you want to download the log file, please use adb command. For example, if you want to copy the log file on "C:\temp" directory, please run the following command.

adb pull /data/data/NLogSample.Android/logs/nlog-sample.csv C:\temp
Shashidhar014 commented 5 years ago

Can I get log files from File explorer?

Shashidhar014 commented 5 years ago

Also, How can I see the log file in Filemanger without rooting?

bricegomis commented 5 years ago

I am also interested to know how to recover logs from an explorer, knowing that users do not know how to use adb. @Shashidhar014 Did you find a solution ?

danmoura17 commented 5 years ago

Same problem here.

pwrp4j1 commented 5 years ago

Hi, I also have issues creating the files in an Android emulator. There are several android explorer apps that can be used to view the root file system but when I navigate to the location of the app, it doesn't contain the log files - this also happens with my project as well as the sample project.