Archisteamfarm: NLog exception when I run ASF as a systemd service (Linux/Mono)

Created on 12 Jul 2016  路  12Comments  路  Source: JustArchiNET/ArchiSteamFarm

If I run ASF directly from the console, it works perfectly fine. However, since I have a server running ASF 24/7, I created a systemd unit file to make ASF restart everytime when there's any issue with the server. Here's my asf.service:

[Unit]
Description=Archi Steam Farm
After=network-online.target

[Service]
Type=simple
User=myself
Environment="MONO_ENV_OPTIONS=--desktop"
ExecStart=/usr/bin/mono /home/myself/ASF.exe
Restart=always
RestartSec=15s

[Install]
WantedBy=multi-user.target
Alias=asf.service

And here's the journalctl log:

Jul 12 01:17:04 myself systemd[1]: Started Archi Steam Farm.
Jul 12 01:17:04 myself systemd[1]: Starting Archi Steam Farm...
Jul 12 01:17:04 myself mono[3660]: [ERROR] FATAL UNHANDLED EXCEPTION: System.TypeInitializationException: The type initializer for 'NLog.Targets.ColoredConsoleTarget' threw an exception. ---> System.UriFormatException: Invalid URI: The hostname could not be parsed.
Jul 12 01:17:04 myself mono[3660]: at System.Uri.Parse (UriKind kind, System.String uriString) <0x405b04e0 + 0x000b3> in <filename unknown>:0
Jul 12 01:17:04 myself mono[3660]: at System.Uri.ParseUri (UriKind kind) <0x405b03c0 + 0x00023> in <filename unknown>:0
Jul 12 01:17:04 myself mono[3660]: at System.Uri..ctor (System.String uriString, Boolean dontEscape) <0x405b0140 + 0x001b7> in <filename unknown>:0
Jul 12 01:17:04 myself mono[3660]: at System.Uri..ctor (System.String uriString) <0x405b0110 + 0x00013> in <filename unknown>:0
Jul 12 01:17:04 myself mono[3660]: at NLog.Config.ConfigurationItemFactory.BuildDefaultFactory () <0x405aaa10 + 0x0013b> in <filename unknown>:0
Jul 12 01:17:04 myself mono[3660]: at NLog.Config.ConfigurationItemFactory.get_Default () <0x405aa930 + 0x00017> in <filename unknown>:0
Jul 12 01:17:04 myself mono[3660]: at NLog.Conditions.ConditionParser.ParseExpression (System.String expressionText) <0x405aa8e0 + 0x0000f> in <filename unknown>:0
Jul 12 01:17:04 myself mono[3660]: at NLog.Conditions.ConditionExpression.op_Implicit (System.String conditionExpressionText) <0x405aa8c0 + 0x0000f> in <filename unknown>:0
Jul 12 01:17:04 myself mono[3660]: at NLog.Targets.ColoredConsoleTarget..cctor () <0x405aa520 + 0x0005f> in <filename unknown>:0
Jul 12 01:17:04 myself mono[3660]: --- End of inner exception stack trace ---
Jul 12 01:17:04 myself mono[3660]: at ArchiSteamFarm.Logging.InitCoreLoggers () <0x405a9f90 + 0x000cf> in <filename unknown>:0
Jul 12 01:17:04 myself mono[3660]: at ArchiSteamFarm.Program.Init (IEnumerable`1 args) <0x4057d3c0 + 0x000eb> in <filename unknown>:0
Jul 12 01:17:04 myself mono[3660]: at ArchiSteamFarm.Program.Main (System.String[] args) <0x40579c30 + 0x0002f> in <filename unknown>:0
Jul 12 01:17:04 myself mono[3660]: exception inside UnhandledException handler: Attempted to access a missing member.
Jul 12 01:17:11 myself systemd[1]: Stopped Archi Steam Farm.

Sorry if I'm missing some required configuration, I just couldn't find it. Server specs:

CentOS Linux release 7.2.1511 (Core)
Mono JIT compiler version 4.2.3 (Stable 4.2.3.4/832de4b Tue Mar 15 11:39:54 EDT 2016)
Copyright (C) 2002-2014 Novell, Inc, Xamarin Inc and Contributors. www.mono-project.com
        TLS:           __thread
        SIGSEGV:       altstack
        Notifications: epoll
        Architecture:  amd64
        Disabled:      none
        Misc:          softdebug
        LLVM:          supported, not enabled.
        GC:            sgen

Thanks in advance.

Most helpful comment

ASF falls back to classic log file when it crashes before NLog initializers, and because it crashed before moving to it's home directory, it tried /log.txt, as systemd probably initialized with home directory set to /. That's misc though.

If you really want to solve this issue, you should report it to NLog devs - https://github.com/NLog/NLog/issues

All 12 comments

After some research, I was able to make the service work again. I just added EnvironmentFile=/etc/environment into the [Service] section:

[Unit]
Description=Archi Steam Farm
After=network-online.target

[Service]
Type=simple
User=myself
EnvironmentFile=/etc/environment
Environment="MONO_ENV_OPTIONS=--desktop"
ExecStart=/usr/bin/mono /home/myself/ASF.exe
Restart=always
RestartSec=15s

[Install]
WantedBy=multi-user.target
Alias=asf.service

Weird because it should not happen, but good to know that you solved it.

Just want to give an update on this:

If you _do not_ have a NLog.config file and you try run ASF as a systemd service, it won't work:

Aug 04 14:54:58 myself systemd[1]: Starting Archi Steam Farm...
Aug 04 14:54:58 myself mono[4590]: [ERROR] FATAL UNHANDLED EXCEPTION: System.TypeInitializationException: The type initializer for 'NLog.Targets.ColoredConsoleTarget' threw an exception. ---> System.UriFormatException: Invalid URI: The hostname could not be parsed.
Aug 04 14:54:58 myself mono[4590]: at System.Uri.Parse (UriKind kind, System.String uriString) <0x41833130 + 0x000ab> in <filename unknown>:0
Aug 04 14:54:58 myself mono[4590]: at System.Uri.ParseUri (UriKind kind) <0x41833010 + 0x00023> in <filename unknown>:0
Aug 04 14:54:58 myself mono[4590]: at System.Uri..ctor (System.String uriString, Boolean dontEscape) <0x41832da0 + 0x001b7> in <filename unknown>:0
Aug 04 14:54:58 myself mono[4590]: at System.Uri..ctor (System.String uriString) <0x41832d70 + 0x00013> in <filename unknown>:0
Aug 04 14:54:58 myself mono[4590]: at NLog.Config.ConfigurationItemFactory.BuildDefaultFactory () <0x4182b580 + 0x00133> in <filename unknown>:0
Aug 04 14:54:58 myself mono[4590]: at NLog.Config.ConfigurationItemFactory.get_Default () <0x4182b4b0 + 0x00017> in <filename unknown>:0
Aug 04 14:54:58 myself mono[4590]: at NLog.Conditions.ConditionParser.ParseExpression (System.String expressionText) <0x4182b460 + 0x0000f> in <filename unknown>:0
Aug 04 14:54:58 myself mono[4590]: at NLog.Conditions.ConditionExpression.op_Implicit (System.String conditionExpressionText) <0x4182b440 + 0x0000f> in <filename unknown>:0
Aug 04 14:54:58 myself mono[4590]: at NLog.Targets.ColoredConsoleTarget..cctor () <0x4182b110 + 0x0004f> in <filename unknown>:0
Aug 04 14:54:58 myself mono[4590]: --- End of inner exception stack trace ---
Aug 04 14:54:58 myself mono[4590]: at ArchiSteamFarm.Logging.InitCoreLoggers () <0x4182abd0 + 0x000b7> in <filename unknown>:0
Aug 04 14:54:58 myself mono[4590]: at ArchiSteamFarm.Program.Init (System.String[] args) <0x417bb3c0 + 0x000cb> in <filename unknown>:0
Aug 04 14:54:58 myself mono[4590]: at ArchiSteamFarm.Program.Main (System.String[] args) <0x417a58e0 + 0x0002f> in <filename unknown>:0
Aug 04 14:54:58 myself mono[4590]: exception inside UnhandledException handler: Access to the path "/log.txt" is denied.
Aug 04 14:55:13 myself systemd[1]: ASF.service holdoff time over, scheduling restart.

I don't know why it happens, and I'm not interested to look into it atm, so if you run into this problem like I did, just create a basic NLog.config from the Wiki page.

Aug 04 14:54:58 myself mono[4590]: exception inside UnhandledException handler: Access to the path "/log.txt" is denied.

I saw that, but I don't understand what would I have to do to get rid of it... Creating a NLog.config file worked.

ASF falls back to classic log file when it crashes before NLog initializers, and because it crashed before moving to it's home directory, it tried /log.txt, as systemd probably initialized with home directory set to /. That's misc though.

If you really want to solve this issue, you should report it to NLog devs - https://github.com/NLog/NLog/issues

Judging by the environment variables in the ASF process created by systemd, $HOME is correctly set, but what do I know... :grin:

Maybe I'll report to them later, thanks.

$HOME might be, but current directory is /.

Mmmm, that makes sense, maybe I can try to set $PWD to see if changes anything.

I don't even know if the current dictory can be controlled by an environment variable, lol

No, as I said - that issue is misc. https://github.com/JustArchi/ArchiSteamFarm/commit/eb886e8ca88a78188de4138d52e3291f007e01d7

It won't solve NLog issue though, only misc ASF one.

BTW, this issue is fixed since ASF V2.2.2.8 - no workaround in form of custom NLog.config is required anymore.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

MrBurrBurr picture MrBurrBurr  路  3Comments

tambry picture tambry  路  4Comments

guihkx picture guihkx  路  3Comments

JustArchi picture JustArchi  路  4Comments

light444 picture light444  路  3Comments