Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Configuration sample issue #7

Open
simmotech opened this issue Feb 12, 2021 · 2 comments
Open

Configuration sample issue #7

simmotech opened this issue Feb 12, 2021 · 2 comments

Comments

@simmotech
Copy link

The configuration sample on the front page didn't work for me - no File sink got created.
Eventually, after a long time!, I worked out that needed to be added to the using section.
I know you guys would have realised that immediately but I am a newbie to serilog.

Also, the Xml extension method allows a null filePath but the XmlSettings ctor throws if it is null.
Can I suggest that a null filePath doesn't throw but instead Configure(), on seeing a null filePath, gets the xml from the config file instead: Its the identical Xml section and still goes to create identical KeyValuePairs, just from a different place.
(I know this works because I copied your code - since it is sealed and non-virtual - and tried it out)

@rsabirov
Copy link
Contributor

@simmotech thank you for reasing issue with sample config. I fixed that.

For second part, I am not sure that I understand what you mean here. May be you can send me PR?

@simmotech
Copy link
Author

simmotech commented Feb 12, 2021

Your code insists that the xml resides in its own file; I was suggesting that with a minor tweak, it could read the same xml from app.config:

<?xml version="1.0" encoding="utf-8"?>
<configuration>

  <configSections>
    <section name="serilog" type="<some simple config section handler>" />
  </configSections>

  <serilog>

    <using>
      <!--
      <add name="Serilog.Enrichers.Thread" />
      <add name="Serilog.Enrichers.Process" />
      <add name="Serilog.Enrichers.Environment" />
    -->
      <add name="Serilog.Sinks.File" />
    </using>

    <enrich>
      <!-- <enricher name="FromLogContext" /> -->
      <!-- <enricher name="WithMachineName" /> -->
      <!-- <enricher name="WithThreadId" /> -->
      <!-- <enricher name="WithProcessId" /> -->
      <!-- <enricher name="WithEnvironmentUserName" /> -->
    </enrich>

    <properties>
      <!-- <property name="Application" value="Sample" /> -->
      <!-- <property name="Path" value="%PATH%" /> -->
    </properties>

    <minimumLevel default="Verbose">
      <override name="Microsoft" level="Warning" />
      <override name="Microsoft.AspNetCore.Mvc" level="Error" />
    </minimumLevel>

    <writeTo>
      <sink name="File">
        <arg name="path" value="%ALLUSERSPROFILE%\SmartLibrarian\Logs\Log.txt" />
        <arg name="outputTemplate" value="{Timestamp:yyyy-MM-dd HH:mm:ss.fff} [{Level:u3}] - {SourceContext:c} - {Message:lj}{NewLine}{Exception}" />
      </sink>
    </writeTo>

  </serilog>
</configuration>

This is the simple configuration section handler I used

	public class ConfigurationSectionHandler: IConfigurationSectionHandler
	{
		object IConfigurationSectionHandler.Create(object parent, object configContext, XmlNode section)
		{
			if (parent != null) throw new ConfigurationException("parent configuration sections are not allowed");

			return section;
		}
	}

So if a null filename is supplied, it tries to read it from here instead rather than throwing an exception.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants