WebAdministrationDsc icon indicating copy to clipboard operation
WebAdministrationDsc copied to clipboard

Possible to set custom logging (x-forwared-for) via xWebAdministration?

Open mhendric opened this issue 7 years ago • 7 comments

I wanted to see if it's possible to configure custom logging via xWebAdministration, and if someone could provide an example on how to use it to configure the below XML snippet which adds the x-forwarded-for header to IIS logs? The assumption is that the logFile block, and everything underneath it, does not exist, and would need to be created and configured. I'm having trouble figuring out how to get xWebConfigPropertyCollection to create new elements, and also to configure something that has more than 2 properties, like the add block below. I also don't know how I'd add an empty block, like the clear block.

From: https://docs.microsoft.com/en-us/iis/configuration/system.applicationhost/sites/site/logfile/customfields/add

<sites>
    <site name="Default Web Site" id="1">
        <logFile logFormat="W3C" logTargetW3C="File, ETW">
            <customFields maxCustomFieldLength="4095">
                <clear />
                <add logFieldName="X-Forwarded-For" sourceName="X_FORWARDED_FOR" 
                    sourceType="RequestHeader" />
            </customFields>
        </logFile>
    </site>   
</sites>

mhendric avatar Jul 16 '18 20:07 mhendric

@regedit32 Can you provide some insight to this maybe?

johlju avatar Jul 17 '18 18:07 johlju

@mhendric Looks like we're missing some documentation on how to do this. Here's an example on how to manage multiple log custom fields with xWebSite:

configuration websitelog
{
    
    Import-DscResource -ModuleName xWebAdministration

    Node localhost
    {
        xWebSite 'testWebSite'
        {
            Name = 'test'
            LogCustomFields    = @(
                MSFT_xLogCustomFieldInformation
                {
                    LogFieldName = 'X-Forwarded-For'
                    SourceName   = 'X_FORWARDED_FOR'
                    SourceType   = 'RequestHeader'
                }
                MSFT_xLogCustomFieldInformation
                {
                    LogFieldName = 'ClientEncoding'
                    SourceName   = 'Accept-Encoding'
                    SourceType   = 'RequestHeader'
                }
            )
        }
    }    
}

The same is possible with xIisLogging if needed at the server level as well. This should be doable with xWebConfigPropertyCollection too, but we are missing documentation on how to manage collections with more than one property. When I get a chance later, I can mock up an example on how to manage a collection with multiple properties.

regedit32 avatar Jul 18 '18 02:07 regedit32

@regedit32 , excellent, thanks! I'll give this a shot.

mhendric avatar Jul 18 '18 15:07 mhendric

A small change to this for anyone who might blindly copy and paste like I did. It should now be MSFT_xLogCustomField not MSFT_xLogCustomFieldInformation

OffColour avatar Nov 05 '18 10:11 OffColour

Hi @OffColour, it depends on which resource you are using and if you're targeting a specific website or applying at the server level.

If configuring log custom fields at the site level, then MSFT_xLogCustomFieldInformation would be used with xWebSite. When configuring at the server level with xIisLogging, then MSFT_xLogCustomField.

It would be a problem to use the same class name in two different resources, so although they both do the same thing for two different resources, the class names are slightly different.

However, if you received an error when using MSFT_xLogCustomFieldInformation with xWebsite, please let me know.

regedit32 avatar Nov 05 '18 13:11 regedit32

@regedit32 Ah, that makes sense now. I'm only using xIisLogging with MSFT_xLogCustomField and all working perfectly. Thanks.

OffColour avatar Nov 12 '18 12:11 OffColour

I know this is an old post, but does the proposed solution add the <clear/ > element?

ThomasHughesIV avatar Jun 25 '21 03:06 ThomasHughesIV