I'm using log4net.ext.json for logging in wcf class library and there is no exe. However I'd like to give appname some alias name e.g. CarService instead of /LM/W3SVC/2/ROOT-1-132599327970820414. How do I configure that?
Current config:
<log4net>
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<encoding value="UTF-8" type="System.Text.UTF8Encoding" />
<file type="log4net.Util.PatternString" value="C:\Logs\WcfLibrary.log" name ="RollingLogFileAppender" />
<appendToFile value="true" />
<rollingStyle value="Date" />
<countDirection value="-1"/>
<datePattern value="yyyy-MM-dd"/>
<logName value="Myapp" />
<applicationName value="BmwService" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.SerializedLayout, log4net.Ext.Json">
<member value="logDateTime%date:yyyy-MM-dd HH:mm:ss:ffff" />
<decorator type="log4net.Layout.Decorators.StandardTypesDecorator, log4net.Ext.Json" />
<member value="hostName" />
<default />
<remove value="date" />
<remove value="ndc" />
<remove value="message" />
<remove value="thread" />
<remove value="exception" />
<member value="logData:messageObject" />
</layout>
</appender>
<root>
<level value="All"/>
<appender-ref ref="RollingLogFileAppender"/>
</root>
</log4net>
I am getting below log:
{"logDateTime":"2021-03-11","hostName":"PC","level":"DEBUG","appname":"/LM/W3SVC/2/ROOT-1-132599327970820414","logger":"Service","logData":{"Message":"Service started","ClassName":"Utils","MethodName":"Start"}}
Expected log is:
{"logDateTime":"2021-03-11","hostName":"PC","level":"DEBUG","appname":"CarService","logger":"Service","logData":{"Message":"Service started","ClassName":"Utils","MethodName":"Start"}}
Check out the various context objects in log4net. You would set this before your first logging call (likely in a constructor, or during initialization). It will persist throughout the lifetime of your thread call, and you would only need to set it again if you're outside of your thread. If you've implemented a wrapper library for your logger, you could also set it there based on some parameter you've exposed.
After this has been set, you should be able to reference "appname" in your appender configuration or mapping.
Also, if you're trying to get the application's actual name, and have mirrored that in your IIS site name, you could use "HostingEnvironment.SiteName" to pass into the thread context.