CoreRcon icon indicating copy to clipboard operation
CoreRcon copied to clipboard

LogAddressPacket does not support milliseconds in Timestamp

Open Cryptoc1 opened this issue 1 year ago • 3 comments

Context: I'm building a custom log ingestion server (for use with logaddress_add_http in CS2), and am using the parsers in this library to handle certain logs being ingested.

Problem: The format of the log "packet" slightly differs when using HTTP:

  • the log text does not start with L
  • the log's timestamp includes milliseconds

As a result, I was unable to use the LogAddressPacket type for parsing log content.

Workaround:

I created the following LogPacket type in my project to handle these differences (note that I'm on net8.0):

public sealed partial record LogPacket( string Body, DateTime Timestamp )
{
    [GeneratedRegex( @"^(\d{2}/\d{2}/\d{4} - \d{2}:\d{2}:\d{2}\.\d{3}) - " )]
    private static partial Regex Parser( );

    public static bool TryParse( string? value, [NotNullWhen( true )] out LogPacket? packet )
    {
        if( !string.IsNullOrWhiteSpace( value ) )
        {
            var match = Parser().Match( value );
            if( match.Success && DateTime.TryParseExact( match.Groups[ 1 ].Value, "MM/dd/yyyy - HH:mm:ss.fff", CultureInfo.InvariantCulture, default, out var timestamp ) )
            {
                var body = value[ match.Groups[ 0 ].Length.. ].Trim();
                if( body.Length is not 0 )
                {
                    packet = new( body, timestamp );
                    return true;
                }
            }
        }

        packet = default;
        return false;
    }
}

The LogPacket.Body can then be used with DefaultParser<> implementations as expected.

I considered opening a PR, but wasn't sure if this use case fit the "scope" of this project.

Cryptoc1 avatar Oct 26 '23 22:10 Cryptoc1