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

Is it possible read the columnOptions from JSON configuration #109

Closed
Jenan opened this issue Jan 31, 2018 · 6 comments
Closed

Is it possible read the columnOptions from JSON configuration #109

Jenan opened this issue Jan 31, 2018 · 6 comments

Comments

@Jenan
Copy link

Jenan commented Jan 31, 2018

Hello,

I have tried to use following configuration structure:

var columnOptions = new ColumnOptions();

            // Don't include the Properties XML column.
            columnOptions.Store.Remove(StandardColumn.Properties);

            // Do include the log event data as JSON.
            columnOptions.Store.Add(StandardColumn.LogEvent);

            Log.Logger = new LoggerConfiguration()
                .WriteTo.MSSqlServer(Configuration.GetConnectionString("DbContext"), "Log", columnOptions: columnOptions,
                    restrictedToMinimumLevel: LogEventLevel.Error)
                .CreateLogger();

Read configuration above from JSON settings:

{
  "Serilog": {
    "MinimumLevel": "Error",
    "WriteTo": [
      {
        "Name": "MSSqlServer",
        "Args": {
          "connectionString": "<our connection string>",
          "tableName": "Log"
        }
      }
    ]
  }
}

How can I specify the columnOptions settings in JSON or how combine the columnOptions with JSON configuration?

I want to be able to change the MinimumLevel from configuration file.

Thank you.

@faisaliyk
Copy link

I would like to know if there is a solution for this.

@tsimbalar
Copy link

@Jenan there is currently no way to specify the column options in JSON format.

You cannot mix configuration for the same sink both in code and in config file, but you can configure the "global" Minimum Level in config file, and the SQLServer sink in code.

This would mean :

  • this in the config file :
{
  "Serilog": {
    "MinimumLevel": "Error"
  }
}
  • this in code :
var columnOptions = new ColumnOptions();

// Don't include the Properties XML column.
columnOptions.Store.Remove(StandardColumn.Properties);

// Do include the log event data as JSON.
columnOptions.Store.Add(StandardColumn.LogEvent);

Log.Logger = new LoggerConfiguration()
        .ReadFrom.Configuration(.... the configurationBuilder.AddJson(...).Build() part)
	.WriteTo.MSSqlServer(Configuration.GetConnectionString("DbContext"), "Log", columnOptions: columnOptions,
		restrictedToMinimumLevel: LogEventLevel.Error)
	.CreateLogger();

The workaround, if you want to be able to configure the connection string and the restrictedToMinimumLevel is to load it your self from the settings, and pass it around in the C# code ...

I hope this helps !

@MV10
Copy link
Contributor

MV10 commented Aug 24, 2018

This is now possible with the latest SQL sink and Serilog.Settings.Configuration packages (dev versions, currently).

@MV10 MV10 closed this as completed Aug 24, 2018
@fatim
Copy link

fatim commented Sep 4, 2018

Could somebody share an example of JSON configuration (in dev version) of removing standard column from store and adding new custom column in AdditionalDataColumns collection ?
i.e this
columnOptions.Store.Remove(StandardColumn.MessageTemplate)
and
columnOptions.AdditionalDataColumns = new Collection<DataColumn> { new DataColumn {DataType = typeof(string), ColumnName = "MyStringColumn"} }

@OculiViridi
Copy link

@fatim Take a look at #129 for JSON configuration and look at #126 for fluent in code configuration.

@Jenan
Copy link
Author

Jenan commented Oct 31, 2018

How can I use these lines via JSON configuration:

        // Don't include the Properties XML column.
            columnOptions.Store.Remove(StandardColumn.Properties);

            // Do include the log event data as JSON.
            columnOptions.Store.Add(StandardColumn.LogEvent);

I have tried something like this - but it doesn't work:

{
				"Name": "MSSqlServer",
				"Args": {
					"connectionString": "connectionString",
					"tableName": "Log",
					"columnOptionsSection": {
						"addStandardColumns": [ "LogEvent" ],
						"removeStandardColumns": [ "Properties" ]
					}
				}
			}

Thanks

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

6 participants